-
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.
This reverts commit 4eaceab.
- Loading branch information
Showing
8 changed files
with
10,351 additions
and
14,360 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
import NextAuth from 'next-auth' | ||
import Providers from 'next-auth/providers' | ||
|
||
// For more information on each option (and a full list of options) go to | ||
// https://next-auth.js.org/configuration/options | ||
export default NextAuth({ | ||
// https://next-auth.js.org/configuration/providers | ||
providers: [ | ||
// Providers.Email({ | ||
// server: process.env.EMAIL_SERVER, | ||
// from: process.env.EMAIL_FROM, | ||
// }), | ||
Providers.GitHub({ | ||
clientId: process.env.GITHUB_ID, | ||
clientSecret: process.env.GITHUB_SECRET, | ||
}), | ||
Providers.Google({ | ||
clientId: process.env.GOOGLE_ID, | ||
clientSecret: process.env.GOOGLE_SECRET, | ||
}), | ||
Providers.LinkedIn({ | ||
clientId: process.env.LINKEDIN_ID, | ||
clientSecret: process.env.LINKEDIN_SECRET, | ||
}), | ||
], | ||
// Database optional. MySQL, Maria DB, Postgres and MongoDB are supported. | ||
// https://next-auth.js.org/configuration/databases | ||
// | ||
// Notes: | ||
// * You must to install an appropriate node_module for your database | ||
// * The Email provider requires a database (OAuth providers do not) | ||
// database: process.env.DATABASE_URL, | ||
|
||
// The secret should be set to a reasonably long random string. | ||
// It is used to sign cookies and to sign and encrypt JSON Web Tokens, unless | ||
// a separate secret is defined explicitly for encrypting the JWT. | ||
secret: process.env.SECRET_COOKIE_PASSWORD, | ||
|
||
session: { | ||
// Use JSON Web Tokens for session instead of database sessions. | ||
// This option can be used with or without a database for users/accounts. | ||
// Note: `jwt` is automatically set to `true` if no database is specified. | ||
jwt: true, | ||
|
||
// Seconds - How long until an idle session expires and is no longer valid. | ||
// maxAge: 30 * 24 * 60 * 60, // 30 days | ||
|
||
// Seconds - Throttle how frequently to write to database to extend a session. | ||
// Use it to limit write operations. Set to 0 to always update the database. | ||
// Note: This option is ignored if using JSON Web Tokens | ||
// updateAge: 24 * 60 * 60, // 24 hours | ||
}, | ||
|
||
// JSON Web tokens are only used for sessions if the `jwt: true` session | ||
// option is set - or by default if no database is specified. | ||
// https://next-auth.js.org/configuration/options#jwt | ||
jwt: { | ||
// A secret to use for key generation (you should set this explicitly) | ||
// secret: 'INp8IvdIyeMcoGAgFGoA61DdBglwwSqnXJZkgz8PSnw', | ||
// Set to true to use encryption (default: false) | ||
// encryption: true, | ||
// You can define your own encode/decode functions for signing and encryption | ||
// if you want to override the default behaviour. | ||
// encode: async ({ secret, token, maxAge }) => {}, | ||
// decode: async ({ secret, token, maxAge }) => {}, | ||
}, | ||
|
||
// You can define custom pages to override the built-in ones. These will be regular Next.js pages | ||
// so ensure that they are placed outside of the '/api' folder, e.g. signIn: '/auth/mycustom-signin' | ||
// The routes shown here are the default URLs that will be used when a custom | ||
// pages is not specified for that route. | ||
// https://next-auth.js.org/configuration/pages | ||
pages: { | ||
// signIn: '/auth/signin', // Displays signin buttons | ||
// signOut: '/auth/signout', // Displays form with sign out button | ||
// error: '/auth/error', // Error code passed in query string as ?error= | ||
// verifyRequest: '/auth/verify-request', // Used for check email page | ||
// newUser: null // If set, new users will be directed here on first sign in | ||
}, | ||
|
||
// Callbacks are asynchronous functions you can use to control what happens | ||
// when an action is performed. | ||
// https://next-auth.js.org/configuration/callbacks | ||
callbacks: { | ||
// async signIn(user, account, profile) { return true }, | ||
async redirect(url, baseUrl) { | ||
return baseUrl | ||
}, | ||
// async session(session, user) { return session }, | ||
// async jwt(token, user, account, profile, isNewUser) { return token } | ||
}, | ||
|
||
// Events are useful for logging | ||
// https://next-auth.js.org/configuration/events | ||
events: {}, | ||
|
||
// Enable debug messages in the console if you are having problems | ||
debug: false, | ||
}) |
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 @@ | ||
// This is an example of to protect an API route | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { getSession } from 'next-auth/client' | ||
|
||
const user = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const session = await getSession({ req }) | ||
|
||
if (session) { | ||
res.send({ | ||
content: | ||
'This is protected content. You can access this content because you are signed in.', | ||
}) | ||
} else { | ||
res.send({ | ||
error: 'You must be sign in to view the protected content on this page.', | ||
}) | ||
} | ||
} | ||
export default user |
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,42 @@ | ||
import { Grid, Typography, CircularProgress } from '@mui/material' | ||
import { useSession } from 'next-auth/client' | ||
import React, { useEffect, useState } from 'react' | ||
|
||
const Profile = () => { | ||
const [content, setContent] = useState() | ||
const [session, loading] = useSession() | ||
const userName = session?.user?.name | ||
// Fetch content from protected route | ||
useEffect(() => { | ||
const fetchData = async () => { | ||
const res = await fetch('/api/user') | ||
const json = await res.json() | ||
if (json.content) { | ||
setContent(json.content) | ||
} | ||
} | ||
fetchData() | ||
}, [session]) | ||
|
||
if (loading) { | ||
return <CircularProgress /> | ||
} | ||
|
||
// If no session exists, display access denied message | ||
if (!session) { | ||
return <Typography>Access Denied</Typography> | ||
} | ||
|
||
return ( | ||
<Grid> | ||
<h1>Protected Page</h1> | ||
<p>{`You're sign in as`}</p> | ||
|
||
<strong>{userName || '\u00a0'}</strong> | ||
<br /> | ||
<strong>{content}</strong> | ||
</Grid> | ||
) | ||
} | ||
|
||
export default Profile |
Oops, something went wrong.