-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(envited.ascs.digital): add next-auth (#42)
- Loading branch information
Showing
24 changed files
with
243 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apps/envited.ascs.digital/app/api/auth/[...nextauth]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import NextAuth from 'next-auth' | ||
import type { NextAuthOptions } from 'next-auth' | ||
import CredentialsProvider from 'next-auth/providers/credentials' | ||
|
||
export const authOptions: NextAuthOptions = { | ||
providers: [ | ||
CredentialsProvider({ | ||
// The name to display on the sign in form (e.g. 'Sign in with...') | ||
name: 'Sign in with Your Credentials', | ||
// The credentials is used to generate a suitable form on the sign in page. | ||
// You can specify whatever fields you are expecting to be submitted. | ||
// e.g. domain, username, password, 2FA token, etc. | ||
// You can pass any HTML attribute to the <input> tag through the object. | ||
credentials: { | ||
pkh: { label: 'Address', type: 'text', placeholder: 'tz...' }, | ||
}, | ||
async authorize(credentials) { | ||
if (!credentials) { | ||
return { | ||
id: '', | ||
pkh: '', | ||
memberId: '', | ||
role: '', | ||
} | ||
} | ||
const { pkh } = credentials | ||
|
||
return { | ||
id: '', | ||
pkh, | ||
memberId: '', | ||
role: '', | ||
} | ||
}, | ||
}), | ||
], | ||
session: { | ||
strategy: 'jwt', | ||
}, | ||
callbacks: { | ||
async jwt({ token, user }) { | ||
if (user) { | ||
token.user = user | ||
} | ||
|
||
return token | ||
}, | ||
async session({ session, token }: { session: any; token: any }) { | ||
session.user.pkh = token.user.pkh | ||
session.user.role = token.user.role | ||
session.user.memberId = token.user.memberId | ||
session.user.id = token.sub | ||
session.user.email = undefined | ||
session.user.image = undefined | ||
return session | ||
}, | ||
}, | ||
} | ||
|
||
const handler = NextAuth(authOptions) | ||
|
||
export { handler as GET, handler as POST } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { getServerSession } from '../../common/session' | ||
import { Header } from '../../modules/Header' | ||
|
||
export default async function Index() { | ||
const session = await getServerSession() | ||
return ( | ||
<> | ||
<Header /> | ||
<main> | ||
<div className="mx-auto max-w-6xl">{session ? JSON.stringify(session) : 'No session'}</div> | ||
</main> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getServerSession } from './session' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { _getServerSession } from './session' | ||
|
||
describe('common/session', () => { | ||
describe('getServerSession', () => { | ||
it('should should fetch a server session with the correct parameters', async () => { | ||
// when ... we want to get the current session server side | ||
// then ... it should call the getServerSession function with the correct parameters | ||
const authOptions = 'AUTH_OPTIONS' as any | ||
const NAGetServerSession = jest.fn().mockResolvedValue('SESSION') | ||
|
||
const session = await _getServerSession(NAGetServerSession)(authOptions)() | ||
|
||
expect(NAGetServerSession).toHaveBeenCalledWith(authOptions) | ||
expect(session).toEqual('SESSION') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { getServerSession as NAGetServerSession, NextAuthOptions } from 'next-auth' | ||
|
||
import { authOptions } from '../../app/api/auth/[...nextauth]/route' | ||
|
||
export const _getServerSession = (NAGetServerSession: any) => (authOptions: NextAuthOptions) => () => | ||
NAGetServerSession(authOptions) | ||
|
||
export const getServerSession = _getServerSession(NAGetServerSession)(authOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.