All Projects → j0lv3r4 → Next Authentication

j0lv3r4 / Next Authentication

Licence: mit
Authentication & Authorization library for the Next.js framework

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
es6
455 projects

Projects that are alternatives of or similar to Next Authentication

Graphql Directive Auth
GraphQL directive for handling auth
Stars: ✭ 120 (+118.18%)
Mutual labels:  authentication, authorization, auth
Mosquitto Go Auth
Auth plugin for mosquitto.
Stars: ✭ 212 (+285.45%)
Mutual labels:  authentication, authorization, auth
Fosite
Extensible security first OAuth 2.0 and OpenID Connect SDK for Go.
Stars: ✭ 1,738 (+3060%)
Mutual labels:  authentication, authorization, auth
Rbac
Hierarchical Role Based Access Control for NodeJS
Stars: ✭ 857 (+1458.18%)
Mutual labels:  authentication, authorization, auth
Awesome Auth
📊 Software and Libraries for Authentication & Authorization
Stars: ✭ 520 (+845.45%)
Mutual labels:  authentication, authorization, auth
Sentinel
A framework agnostic authentication & authorization system.
Stars: ✭ 1,354 (+2361.82%)
Mutual labels:  authentication, authorization, auth
Huge
Simple user-authentication solution, embedded into a small framework.
Stars: ✭ 2,125 (+3763.64%)
Mutual labels:  authentication, authorization, auth
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (+43.64%)
Mutual labels:  authentication, authorization, auth
Laravel5.7 Vue Cli3 Boilerplate
Boilerplate / Starter kit. Laravel 5.7, Vue CLI 3 — Authentication with Email Verification. REST API.
Stars: ✭ 52 (-5.45%)
Mutual labels:  authentication, authorization, auth
Annon.api
Configurable API gateway that acts as a reverse proxy with a plugin system.
Stars: ✭ 306 (+456.36%)
Mutual labels:  authentication, authorization, auth
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (+32.73%)
Mutual labels:  authentication, authorization, auth
Php Auth
Authentication for PHP. Simple, lightweight and secure.
Stars: ✭ 713 (+1196.36%)
Mutual labels:  authentication, authorization, auth
Vuejs2 Authentication Tutorial
Stars: ✭ 144 (+161.82%)
Mutual labels:  authentication, authorization, auth
Nextjs Redux Firebase Authentication
Boilerplate Project for Authentication with Firebase in NextJs and Redux
Stars: ✭ 90 (+63.64%)
Mutual labels:  nextjs, authentication, authorization
Social Core
Python Social Auth - Core
Stars: ✭ 618 (+1023.64%)
Mutual labels:  authentication, authorization, auth
Aws Serverless Auth Reference App
Serverless reference app and backend API, showcasing authentication and authorization patterns using Amazon Cognito, Amazon API Gateway, AWS Lambda, and AWS IAM.
Stars: ✭ 724 (+1216.36%)
Mutual labels:  authentication, authorization, auth
Kratos Selfservice Ui React Native
A reference implementation of an app using ORY Kratos for auth (login), sign up (registration), profile settings (update password), MFA/2FA, account recovery (password reset), and more for React Native. This repository is available as an expo template!
Stars: ✭ 24 (-56.36%)
Mutual labels:  authentication, auth
Fernet Java8
Java 8 implementation of the Fernet Specification
Stars: ✭ 24 (-56.36%)
Mutual labels:  authentication, authorization
Php Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
Stars: ✭ 865 (+1472.73%)
Mutual labels:  authorization, auth
Keeper
Stars: ✭ 23 (-58.18%)
Mutual labels:  authentication, authorization

Next Authentication

FOSSA Status

Authentication & Authorization for Next.js

next-authentication provides a set of functions and middlewares to implement Authentication, Authorization and session management in Next.js applications.

Usage

Setup:

// Setup
// file: lib/auth.js

import bcrypt from 'bcrypt';
import { nextAuth, AuthError } from 'next-authentication';
import { User } from '../user/model';

const nextAuthOptions = {   
  cookieName: 'auth-token',
  // Pseudo code that verifies a user in a fictitious database
  verify: async (username, password) => {
      try {
        const user = await User.query().findOne({ username });

        if (!user) {
          throw new AuthError('User does not exist', 404);
        }

        const valid = bcrypt.compareSync(password, user.password);

        if (!valid) {
          throw new AuthError('Invalid credentials', 403);
        }

        return { user: user.username }
      } catch (error) {
        throw new AuthError(`Error trying to verifying the user: ${error.message}`, 500);
      }
  },
  secret: process.env.SECRET || 'alongsecretvaluethatsatleast16chars'
}

export const { authenticate, authorize } = nextAuth(nextAuthOptions);

Login:

// Authenticate
// file: pages/api/login.js

import { authenticate } from '../lib/auth.js'

const handler = (req, res) => {
  res.status(200).json({ message: 'User logged in', user: req.user });
}

export default authenticate(handler);

Restricted content:

// Authorize
// file: pages/api/restricted-content.js

import { authorize } from '../lib/auth.js';

const handler = (req, res) => {
  console.log('is authorized', res.isAuthorized);
  res.status(200).json({ user: res.user })
}

export default authorize(handler);

API

handler(req, res)

A requestListener function that is executed each time an API route gets a request.

This is not a next-authentication method, but rather a definition about a parameter we use through the documentation. It’s handy to have the definition for reference.

Usage:

// file: pages/api/ok.js
const handler = (req, res) => {
  res.end(JSON.stringify({ message: 'ok' }));
}

export default handler;

nextAuth({ verify, secret, cookieUserOptions, [redirectOnError, redirectUrl] })

The main function of the library that takes an option object and returns an object with the functions you to use for authentication, authorization, and logout users.

verify(username, password) (required)
  • username <string> (required)
  • password <string> (required)
  • Returns an object with at least a username element. e.g., { username: 'jolvera' }

A function that takes a username and a password and must return an object containing at least the key username. The function should run the logic to verify the authenticity of a user's identity.

externalServer <boolean> (optional)
  • Default: false
cookieName <string> (optional)
  • Default: "next-authentication-token"
secret <string> (required)

A secret string that’s at least 16 characters long.

cookieUserOptions <Object> (optional)
  • Default: { httpOnly: true, maxAge: 60 * 60 * 24, path: "/" }

Same options as cookie.serialize.

redirectOnError <boolean> (optional)
  • Default: true

If true, next-authentication redirects the user to redirectUrl when:

  • The user provides invalid credentials
  • The user logs out
  • There is an unknown error
redirectUrl <string> (optional)
  • Default: /login

URL to redirect the user to if redirectOnError is true.

authenticate(handler, authenticateOptions)

A function middleware that verifies the user and creates a cookie session.

You can use the function directly, but the recommended way is through nextAuth since the options are setup once there and can be use everywhere. If you use the function directly you will have to call the function with all parameters every time you use it.

handler(req, res)

authenticateOptions <Object>

authorize(handler, authorizeOptions)

Validates a session.

handler(req, res)

authorizeOptions <Object>

logout(handler, logoutOptions)

Destroys the user session and redirects the user based on redirectOnError and redirectUrl.

handler(req, res)

logoutOptions <Object>

AuthError(message, status)

Custom error class for Authorization errors.

message <string>

Error message to use in a response <ServerResponse> object.

status <integer>

  • Default: 401

Server status code to use in a response <ServerResponse> object.

Installation

With npm:

$ npm i next-authentication --save

License

FOSSA Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].