Skip to content

Commit

Permalink
refacotr: simplify default callbacks lib file
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Dec 11, 2020
1 parent 8331370 commit d04185c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cookie from './lib/cookie'
import callbackUrlHandler from './lib/callback-url-handler'
import parseProviders from './lib/providers'
import events from './lib/events'
import callbacks from './lib/callbacks'
import * as defaultCallbacks from './lib/defaultCallbacks'
import providers from './routes/providers'
import signin from './routes/signin'
import signout from './routes/signout'
Expand All @@ -21,7 +21,7 @@ if (!process.env.NEXTAUTH_URL) {
logger.warn('NEXTAUTH_URL', 'NEXTAUTH_URL environment variable not set')
}

async function NextAuth (req, res, userSuppliedOptions) {
async function NextAuthHandler (req, res, userSuppliedOptions) {
// To the best of my knowledge, we need to return a promise here
// to avoid early termination of calls to the serverless function
// (and then return that promise when we are done) - eslint
Expand Down Expand Up @@ -139,7 +139,7 @@ async function NextAuth (req, res, userSuppliedOptions) {

// Callback functions
const callbacksOptions = {
...callbacks,
...defaultCallbacks,
...userSuppliedOptions.callbacks
}

Expand Down Expand Up @@ -314,10 +314,10 @@ async function NextAuth (req, res, userSuppliedOptions) {
})
}

export default async (...args) => {
export default async function NextAuth (...args) {
if (args.length === 1) {
return (req, res) => NextAuth(req, res, args[0])
return (req, res) => NextAuthHandler(req, res, args[0])
}

return NextAuth(...args)
return NextAuthHandler(...args)
}
51 changes: 20 additions & 31 deletions src/server/lib/callbacks.js → src/server/lib/defaultCallbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
* requests to sign in and again when they activate the link in the sign in
* email.
*
* @param {object} profile User profile (e.g. user id, name, email)
* @param {object} account Account used to sign in (e.g. OAuth account)
* @param {object} metadata Provider specific metadata (e.g. OAuth Profile)
* @return {boolean|object} Return `true` (or a modified JWT) to allow sign in
* Return `false` to deny access
* @param {object} profile User profile (e.g. user id, name, email)
* @param {object} account Account used to sign in (e.g. OAuth account)
* @param {object} metadata Provider specific metadata (e.g. OAuth Profile)
* @return {Promise<boolean|never>} Return `true` (or a modified JWT) to allow sign in
* Return `false` to deny access
*/
const signIn = async (profile, account, metadata) => {
const isAllowedToSignIn = true
if (isAllowedToSignIn) {
return Promise.resolve(true)
} else {
return Promise.resolve(false)
}
export async function signIn () {
return true
}

/**
Expand All @@ -31,12 +26,13 @@ const signIn = async (profile, account, metadata) => {
*
* @param {string} url URL provided as callback URL by the client
* @param {string} baseUrl Default base URL of site (can be used as fallback)
* @return {string} URL the client will be redirect to
* @return {Promise<string>} URL the client will be redirect to
*/
const redirect = async (url, baseUrl) => {
return url.startsWith(baseUrl)
? Promise.resolve(url)
: Promise.resolve(baseUrl)
export async function redirect (url, baseUrl) {
if (url.startsWith(baseUrl)) {
return url
}
return baseUrl
}

/**
Expand All @@ -45,31 +41,24 @@ const redirect = async (url, baseUrl) => {
*
* @param {object} session Session object
* @param {object} token JSON Web Token (if enabled)
* @return {object} Session that will be returned to the client
* @return {Promise<object>} Session that will be returned to the client
*/
const session = async (session, token) => {
return Promise.resolve(session)
export async function session (session) {
return session
}

/**
* This callback is called whenever a JSON Web Token is created / updated.
* e.g. On sign in, `getSession()`, `useSession()`, `/api/auth/session` (etc)
*
* On initial sign in, the raw oAuthProfile is passed if the user is signing in
* On initial sign in, the raw OAuthProfile is passed if the user is signing in
* with an OAuth provider. It is not avalible on subsequent calls. You can
* take advantage of this to persist additional data you need to in the JWT.
*
* @param {object} token Decrypted JSON Web Token
* @param {object} oAuthProfile OAuth profile - only available on sign in
* @return {object} JSON Web Token that will be saved
* @return {Promise<object>} JSON Web Token that will be saved
*/
const jwt = async (token, oAuthProfile) => {
return Promise.resolve(token)
}

export default {
signIn,
redirect,
session,
jwt
export async function jwt (token) {
return token
}

0 comments on commit d04185c

Please sign in to comment.