Skip to content

Parse-server module for implementing an OAuth2 Social Media Login with Express in Node.js

License

Notifications You must be signed in to change notification settings

gimdongwoo/parse-oauth2-sns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parse OAuth2 SNS (Social Media)

npm version

Node.JS & Express module for social media (Facebook, Google, Instagram) auth and login to parse-server. Plus, Korean SNS (Social Media) supports (Naver, Daum, Kakao)

Install

npm install --save parse-oauth2-sns

np How to Use


For Application

  1. Use internal browser (like Android Webview)

  2. Open auth url : /facebook/auth

http://__your_host__/oauth2/facebook/auth
  1. Check url changed to '/callback'

  2. Then url chenged to '/callback', get authdata from body.

// URL : facebook/callback
{"access_token":"...","expiration_date":"..."}

For Web

  1. Open auth url with URL in callback parameter : /facebook/auth?callback=URL
window.location.href =
  "http://__your_host__/oauth2/facebook/auth?callback=" +
  encodeURIComponent("/loginCallback?type=facebook");
Params Type Description
callback string callback url. Redirected after authentication
host string If using proxy, can change api url host. ex) host=your_host/api
  1. Then URL is called, get authdata from querystring.
http://__host__/loginCallback?type=facebook&access_token=...& expiration_date=...

Routes

Facebook Routes

  • /facebook/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to Facebook OAuth page

  • /facebook/callback

    • request : from facebook OAuth page

    • response : json

    {"access_token":"...","expiration_date":"..."}
  • /facebook/login

    • request [post] : json (facebook auth info)
    {"access_token":"...","expiration_date":"..."}
    • response : parse-serve user object (username equal to facebook email)
    {"objectId": "ziJdB2jBul", "username": "__facebook.email__", authData, ...}

Google Routes

  • /google/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to Google OAuth page

  • /google/callback

    • request : from google OAuth page

    • response : json

    {"access_token":"...","expiration_date":"..."}
  • /google/login

    • request [post] : json (google auth info)
    {"access_token":"...","expiration_date":"..."}
    • response : parse-serve user object (username equal to google email)
    {"objectId": "ziJdB2jBul", "username": "__google.email__", authData, ...}

Instagram Routes

  • /instagram/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to Instagram OAuth page

  • /instagram/callback

    • request : from instagram OAuth page

    • response : json

    {"access_token":"...","user":"..."}
  • /instagram/login

    • request [post] : json (instagram auth info)
    {"access_token":"..."}
    • response : parse-server user object (username equal to instagram username)
    {"objectId": "ziJdB2jBul", "username": "__instagram.username__", authData, ...}
  • /instagram/link : parse-server user link to instagram user.

    • request [post] : instagram token and parse-server user info.
    {"access_token":"", "username": "__parse-server user.username__"}
    • response : parse-server user object linked instagram
    {"objectId": "ziJdB2jBul", "username": "__username__", authData, ...}
  • /instagram/recent : get recent post from instagram

    • request [get] : userId (parse-server user.objectId)

    • response : instagram posts

    [{images, caption, comments, ...}, ...]

Naver Routes

  • /naver/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to naver OAuth page

  • /naver/callback

    • request : from naver OAuth page

    • response : json

    {"access_token":"...","expiration_date":"..."}
  • /naver/login

    • request [post] : json (naver auth info)
    {"access_token":"...","expiration_date":"..."}
    • response : parse-serve user object (username equal to naver email)
    {"objectId": "ziJdB2jBul", "username": "__naver.email__", authData, ...}

Daum Routes

  • /daum/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to daum OAuth page

  • /daum/callback

    • request : from daum OAuth page

    • response : json

    {"access_token":"...","expiration_date":"..."}
  • /daum/login

    • request [post] : json (daum auth info)
    {"access_token":"...","expiration_date":"..."}
    • response : parse-server user object (username equal to daum userid, not email provided)
    {"objectId": "ziJdB2jBul", "username": "__daum.userid__", authData, ...}

Kako Routes

  • /kakao/auth

    • request [get] : callback (url, option), host (url, option)

    • response : redirect to kakao OAuth page

  • /kakao/callback

    • request : from kakao OAuth page

    • response : json

    {"access_token":"...","expiration_date":"..."}
  • /kakao/login

    • request [post] : json (kakao auth info)
    {"access_token":"...","expiration_date":"..."}
    • response : parse-server user object (username equal to kakao email or kakao userid)
    {"objectId": "ziJdB2jBul", "username": "__kakao.(kaccount_email||id)__", authData, ...}

Initialize

Setup up process.env

  • It's work with parse-rest-nodejs.

    // Recommend to use 'better-npm-run'.
    process.env.SERVER_URL = "http://__host__:__port__/parse";
    process.env.APP_ID = "__app_id__";
    process.env.MASTER_KEY = "__master_key__";
    process.env.FB_APPIDS = "__fb_key__";
    process.env.FB_SECRETS = "__fb_secret__";
    process.env.GOOGLE_APPIDS = "__google_key__";
    process.env.GOOGLE_SECRETS = "__goole_secret__";
    process.env.INSTA_APPIDS = "__insta_key__";
    process.env.INSTA_SECRETS = "__insta_secret__";
    process.env.NAVER_APPIDS = "__naver_key__";
    process.env.NAVER_SECRETS = "__naver_secret__";
    process.env.DAUM_APPIDS = "__daum_key__";
    process.env.DAUM_SECRETS = "__daum_secret__";
    process.env.KAKAO_RESTKEY = "__kakao_restkey__";
    process.env.KAKAO_SECRETS = "__kakao_secret__";

Router using Express

  • load module

    // es6
    import express from "express";
    import session from "express-session";
    import SocialOAuth2 from "parse-oauth2-sns";
    import bodyParser from "body-parser";
    // es5
    var express = require("express");
    var session = require("session");
    var SocialOAuth2 = require("parse-oauth2-sns").default;
    var bodyParser = require("body-parser");
  • create object

    // for use req.session
    app.use(
      session({
        secret: "___secret_key_for_session___",
        resave: false,
        saveUninitialized: false
        // cookie: { maxAge: 60000 }
      })
    );
    // for use req.body
    app.use(bodyParser.json());
    
    // OAuth2
    app.use("/oauth2", SocialOAuth2.create({ path: "/oauth2" }));
    // OR OAuth2 + userObject Handler
    // Handler is normal function or promise function.
    app.use('/oauth2', SocialOAuth2.create({ path: '/oauth2', userHandler: function(req, user) { ...  return user; } }));
  • Full code is in test.js

Addon Features

User

  • user block/ban

    • if user.isBanned value is setted, user can't login.

About

Parse-server module for implementing an OAuth2 Social Media Login with Express in Node.js

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published