-
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 login buttons (#46)
- Loading branch information
Showing
17 changed files
with
200 additions
and
103 deletions.
There are no files selected for viewing
57 changes: 1 addition & 56 deletions
57
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
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,19 @@ | ||
import { _signIn } from './auth' | ||
|
||
describe('common/auth/auth', () => { | ||
describe('signIn', () => { | ||
it('should call the sign in method with the expected parameters', async () => { | ||
// when ... we want to sign a user in | ||
// then ... it should call the sign in method with the expected parameters | ||
const NASignIn = jest.fn().mockResolvedValue('SIGNED_IN') | ||
const pkh = 'PKH' | ||
|
||
const session = await _signIn(NASignIn)({ pkh }) | ||
expect(NASignIn).toHaveBeenCalledWith('credentials', { | ||
pkh, | ||
callbackUrl: '/dashboard', | ||
}) | ||
expect(session).toEqual('SIGNED_IN') | ||
}) | ||
}) | ||
}) |
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,86 @@ | ||
import type { NextAuthOptions } from 'next-auth' | ||
import CredentialsProvider from 'next-auth/providers/credentials' | ||
import { signIn as NASignIn, signOut as NASignOut } from 'next-auth/react' | ||
import { match } from 'ts-pattern' | ||
|
||
import { Role } from '../types' | ||
|
||
export const authOptions: NextAuthOptions = { | ||
pages: { | ||
error: '/', | ||
signIn: '/', | ||
}, | ||
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: '', | ||
role: '', | ||
} | ||
} | ||
const { pkh } = credentials | ||
|
||
return match(pkh) | ||
.with('tz1USER', () => ({ | ||
id: '1', | ||
pkh: 'tz1USER', | ||
role: Role.user, | ||
})) | ||
.with('tz1PRINCIPAL', () => ({ | ||
id: '1', | ||
pkh: 'tz1PRINCIPAL', | ||
role: Role.principal, | ||
})) | ||
.with('tz1NO_USER', () => null) | ||
.otherwise(() => null) | ||
}, | ||
}), | ||
], | ||
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.id = token.sub | ||
session.user.email = undefined | ||
session.user.image = undefined | ||
return session | ||
}, | ||
}, | ||
} | ||
|
||
export const _signIn = | ||
(NASignIn: any) => | ||
({ pkh }: { pkh: string }) => | ||
NASignIn('credentials', { | ||
pkh, | ||
callbackUrl: '/dashboard', | ||
}) | ||
|
||
export const signIn = _signIn(NASignIn) | ||
|
||
export const signOut = () => | ||
NASignOut({ | ||
callbackUrl: '/', | ||
}) |
1 change: 1 addition & 0 deletions
1
...ited.ascs.digital/common/session/index.ts → ...envited.ascs.digital/common/auth/index.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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { getServerSession } from './session' | ||
export { signIn, signOut } from './auth' |
4 changes: 2 additions & 2 deletions
4
...cs.digital/common/session/session.test.ts → ....ascs.digital/common/auth/session.test.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
2 changes: 1 addition & 1 deletion
2
...ed.ascs.digital/common/session/session.ts → ...vited.ascs.digital/common/auth/session.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
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 |
---|---|---|
@@ -1,10 +1,2 @@ | ||
export { | ||
Language, | ||
Columns, | ||
Size, | ||
ColorScheme, | ||
} from './types' | ||
export type { | ||
Action, | ||
Obj, | ||
} from './types' | ||
export { Language, Columns, Size, ColorScheme, Role } from './types' | ||
export type { Action, Obj } from './types' |
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,30 @@ | ||
'use client' | ||
|
||
import { Button } from '@envited-marketplace/design-system' | ||
import React, { FC } from 'react' | ||
|
||
import { signOut } from '../../common/auth' | ||
import { Role } from '../../common/types' | ||
|
||
interface DashboardProps { | ||
id: string | ||
address: string | ||
role: Role | ||
} | ||
|
||
export const Dashboard: FC<DashboardProps> = ({ id, address, role }) => { | ||
return ( | ||
<div> | ||
<h1 className="text-3xl font-bold mb-5">You are logged in:</h1> | ||
<dl className="mb-10"> | ||
<dt className="font-bold">ID</dt> | ||
<dd className="ml-5 italic">{id}</dd> | ||
<dt className="font-bold">Address</dt> | ||
<dd className="ml-5 italic">{address}</dd> | ||
<dt className="font-bold">Role</dt> | ||
<dd className="ml-5 italic">{role}</dd> | ||
</dl> | ||
<Button onClick={signOut}>Sign out</Button> | ||
</div> | ||
) | ||
} |
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 { Dashboard } from './Dashboard' |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import '@testing-library/jest-dom' | ||
import { render } from '@testing-library/react' | ||
|
||
import { Header } from './Header' | ||
|
||
describe('Header', () => { | ||
it('should render successfully', () => { | ||
const { baseElement } = render(<Header />) | ||
expect(baseElement).toBeTruthy() | ||
// when ... rendering component | ||
// then ... should render as expected | ||
const { getByText } = render(<Header />) | ||
expect(getByText('Connect')).toBeInTheDocument() | ||
}) | ||
}) |
43 changes: 19 additions & 24 deletions
43
apps/envited.ascs.digital/modules/HeroHeader/HeroHeader.tsx
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
10 changes: 5 additions & 5 deletions
10
apps/envited.ascs.digital/modules/ThemeToggle/ThemeToggle.ui.test.tsx
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import '@testing-library/jest-dom' | ||
import { render } from '@testing-library/react' | ||
import React from 'react' | ||
import TestRenderer from 'react-test-renderer' | ||
|
||
import ThemeToggle from './ThemeToggle' | ||
|
||
describe('modules/ThemeToggle', () => { | ||
describe('render', () => { | ||
it('should render as expected', async () => { | ||
// when ... rendering component | ||
const component = TestRenderer.create(<ThemeToggle />) | ||
|
||
// then ... should render with expected element type | ||
const tree = component.toJSON() as any | ||
expect(tree.children[0].type).toEqual('svg') | ||
|
||
const { getByRole } = render(<ThemeToggle />) | ||
expect(getByRole('button')).toBeInTheDocument() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.