Git Product home page Git Product logo

server-auth's Introduction

Server Auth

An authentication plugin for server.js. So far it handles Github and Twitter login.

import server from "server";
import auth from "@server/auth";

server.plugins.push(auth);

server(ctx => `Hello ${ctx.user ? ctx.user.name : "Anonymous"}`);

Getting Started

We are going to see how to setup Github Login. First install server and this plugin:

npm i server @server/auth

Setup your app in Github and fill in the secrets in your .env file:

GITHUB_ID=
GITHUB_SECRET=

Include the plugin in your main app alongside with a small route with some helper just to see the data:

import server from "server";
import auth from "@server/auth";

server.plugins.push(auth);

const home = ctx => {
  console.log(ctx.user);
  return `Hello ${ctx.user ? ctx.user.name : "Anonymous"}`;
};

server(get("/", home));

Finally start your server with node ., and visit http://localhost:3000/login/github to login into your app. The homepage will show your name, and the terminal all of your info.

Database Integration

The default shown in the Getting Started is the setup just for login. When you have a DB you need to write some extra methods to connect Auth to your specific database data and to the session/cookies. This is achieved with these (all of them are async):

  • findUser(profile) => user: given the profile retrieved from the social network, this function will return the user from your database. You can make the match however you prefer; through the profile.email, or profile.username, or profile.id depending on your needs and whether you have one or multiple providers.
  • createUser(profile) => user: given the profile retrieved from the social network, it creates a user in the database and returns this newly created user.
  • serialize(user) => id: given the user from your database, extract a uniquely identifying ID. This is usually something like the user.id, user._id with Mongoose, or similar. That ID will be stored in the session store for later usage.
  • deserialize(id) => user: given the id extracted from the previous serialization method, this method finds the full user from your database.
import server from "server";
import auth from "@server/auth";

const { get } = server.router;

// Add it to the list of plugins
server.plugins.push(auth);

// Define these 3-4 methods to access your local DB:
const findUser = profile => { ... };
const createUser = profile => { ... };
const serialize = user => user.id;
const deserialize = id => { ... };

// Initialize server.js as usual, passing the options for auth
server(
  { auth: { findUser, createUser, serialize, deserialize } },
  get('/', ctx => `Hello ${ctx.user ? ctx.user.name : 'Anonymous'}`),
);

server-auth's People

Contributors

franciscop avatar

Stargazers

Jacin Fleta de Castro avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.