mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Unified API for all of our user-facing methods. NOTE: `events.error` has been removed. This method has never been called in the core, so it did actually nothing. If you want to log errors to a third-party, check out the [`logger`](https://next-auth.js.org/configuration/options#logger) option instead. BREAKING CHANGE: Two event signatures changed to use named params, `signOut` and `updateUser`: ```diff // [...nextauth].js ... events: { - signOut(tokenOrSession), + signOut({ token, session }), // token if using JWT, session if DB persisted sessions. - updateUser(user) + updateUser({ user }) } ```
253 lines
5.8 KiB
TypeScript
253 lines
5.8 KiB
TypeScript
import Providers, { OAuthConfig } from "next-auth/providers"
|
|
import { Adapter } from "next-auth/adapters"
|
|
import NextAuth, * as NextAuthTypes from "next-auth"
|
|
import { IncomingMessage, ServerResponse } from "http"
|
|
import { Socket } from "net"
|
|
import { NextApiRequest, NextApiResponse } from "internals/utils"
|
|
import { InternalOptions } from "internals"
|
|
|
|
const req: NextApiRequest = Object.assign(new IncomingMessage(new Socket()), {
|
|
query: {},
|
|
cookies: {},
|
|
body: {},
|
|
env: {},
|
|
})
|
|
|
|
const res: NextApiResponse = Object.assign(new ServerResponse(req), {
|
|
send: (body: string) => undefined,
|
|
json: (body: string) => undefined,
|
|
status: (code: number) => res,
|
|
redirect: (statusOrUrl: number | string, url?: string) => res as any,
|
|
setPreviewData: (data: object | string) => res,
|
|
clearPreviewData: () => res,
|
|
})
|
|
|
|
const pageOptions = {
|
|
signin: "path/to/signin",
|
|
signout: "path/to/signout",
|
|
error: "path/to/error",
|
|
verifyRequest: "path/to/verify",
|
|
newUsers: "path/to/signup",
|
|
}
|
|
|
|
const simpleConfig = {
|
|
providers: [
|
|
Providers.GitHub({
|
|
clientId: "123",
|
|
clientSecret: "123",
|
|
scope:
|
|
"user public_repo repo repo_deployment repo:status read:repo_hook read:org read:public_key read:gpg_key",
|
|
}),
|
|
],
|
|
}
|
|
|
|
const exampleUser: NextAuthTypes.User = {
|
|
name: "",
|
|
image: "",
|
|
email: "",
|
|
}
|
|
|
|
const exampleSession: NextAuthTypes.Session = {
|
|
userId: "",
|
|
accessToken: "",
|
|
sessionToken: "",
|
|
}
|
|
|
|
const exampleVerificationRequest = {
|
|
id: "",
|
|
identifier: "",
|
|
token: "",
|
|
expires: new Date(),
|
|
}
|
|
|
|
const MyAdapter: Adapter<Record<string, unknown>> = () => {
|
|
return {
|
|
async getAdapter(appOptions: InternalOptions) {
|
|
return {
|
|
async createUser(profile) {
|
|
return exampleUser
|
|
},
|
|
async getUser(id) {
|
|
return exampleUser
|
|
},
|
|
async getUserByEmail(email) {
|
|
return exampleUser
|
|
},
|
|
async getUserByProviderAccountId(providerId, providerAccountId) {
|
|
return exampleUser
|
|
},
|
|
async updateUser(user) {
|
|
return exampleUser
|
|
},
|
|
async linkAccount(
|
|
userId,
|
|
providerId,
|
|
providerType,
|
|
providerAccountId,
|
|
refreshToken,
|
|
accessToken,
|
|
accessTokenExpires
|
|
) {
|
|
return undefined
|
|
},
|
|
async createSession(user) {
|
|
return exampleSession
|
|
},
|
|
async getSession(sessionToken) {
|
|
return exampleSession
|
|
},
|
|
async updateSession(session, force) {
|
|
return exampleSession
|
|
},
|
|
async deleteSession(sessionToken) {
|
|
return undefined
|
|
},
|
|
async createVerificationRequest(email, url, token, secret, provider) {
|
|
return undefined
|
|
},
|
|
async getVerificationRequest(
|
|
email,
|
|
verificationToken,
|
|
secret,
|
|
provider
|
|
) {
|
|
return exampleVerificationRequest
|
|
},
|
|
async deleteVerificationRequest(
|
|
email,
|
|
verificationToken,
|
|
secret,
|
|
provider
|
|
) {
|
|
return undefined
|
|
},
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
const client = {} // Create a fake db client
|
|
|
|
const allConfig: NextAuthTypes.NextAuthOptions = {
|
|
providers: [
|
|
Providers.Twitter({
|
|
clientId: "123",
|
|
clientSecret: "123",
|
|
}),
|
|
],
|
|
debug: true,
|
|
secret: "my secret",
|
|
session: {
|
|
jwt: true,
|
|
maxAge: 365,
|
|
updateAge: 60,
|
|
},
|
|
jwt: {
|
|
secret: "secret-thing",
|
|
maxAge: 365,
|
|
encryption: true,
|
|
signingKey: "some-key",
|
|
encryptionKey: "some-key",
|
|
encode: async () => "foo",
|
|
decode: async () => ({}),
|
|
},
|
|
pages: pageOptions,
|
|
callbacks: {
|
|
async signIn({ user, account, email, credentials, profile }) {
|
|
return true
|
|
},
|
|
async redirect({ url, baseUrl }) {
|
|
return "path/to/foo"
|
|
},
|
|
async session({ session, user, token }) {
|
|
return session
|
|
},
|
|
async jwt({ token, user, account, profile, isNewUser }) {
|
|
return token
|
|
},
|
|
},
|
|
events: {
|
|
async signIn(message) {
|
|
return undefined
|
|
},
|
|
async signOut(message) {
|
|
return undefined
|
|
},
|
|
async createUser(message) {
|
|
return undefined
|
|
},
|
|
async updateUser(message) {
|
|
return undefined
|
|
},
|
|
async linkAccount(message) {
|
|
return undefined
|
|
},
|
|
async session(message) {
|
|
return undefined
|
|
},
|
|
},
|
|
adapter: MyAdapter(client),
|
|
useSecureCookies: true,
|
|
cookies: {
|
|
sessionToken: {
|
|
name: "__Secure-next-auth.session-token",
|
|
options: {
|
|
httpOnly: true,
|
|
sameSite: true as true,
|
|
path: "/",
|
|
secure: true,
|
|
domain: "foo.com",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
const customProvider: OAuthConfig<{
|
|
id: string
|
|
name: string
|
|
email: string
|
|
picture: string
|
|
}> = {
|
|
id: "google",
|
|
name: "Google",
|
|
type: "oauth",
|
|
version: "2.0",
|
|
scope:
|
|
"https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
|
|
params: { grant_type: "authorization_code" },
|
|
accessTokenUrl: "https://accounts.google.com/o/oauth2/token",
|
|
requestTokenUrl: "https://accounts.google.com/o/oauth2/auth",
|
|
authorizationUrl:
|
|
"https://accounts.google.com/o/oauth2/auth?response_type=code",
|
|
profileUrl: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
|
|
async profile(profile, tokens) {
|
|
return {
|
|
id: profile.id,
|
|
name: profile.name,
|
|
email: profile.email,
|
|
image: profile.picture,
|
|
}
|
|
},
|
|
clientId: "",
|
|
clientSecret: "",
|
|
}
|
|
|
|
const customProviderConfig = {
|
|
providers: [customProvider],
|
|
}
|
|
|
|
// $ExpectType void | Promise<void>
|
|
NextAuth(simpleConfig)
|
|
|
|
// $ExpectType void | Promise<void>
|
|
NextAuth(allConfig)
|
|
|
|
// $ExpectType void | Promise<void>
|
|
NextAuth(customProviderConfig)
|
|
|
|
// $ExpectType void | Promise<void>
|
|
NextAuth(req, res, simpleConfig)
|
|
|
|
// $ExpectType void | Promise<void>
|
|
NextAuth(req, res, allConfig)
|