-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathadapter.ts
More file actions
63 lines (58 loc) · 1.82 KB
/
adapter.ts
File metadata and controls
63 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
getUserById,
addUser,
getUserByEmail,
updateUser,
addOauthAccount,
getOauthAccountByProviderId,
} from '@/lib/data/legacy/iam/users';
import {
createVerificationToken,
deleteVerificationToken,
getVerificationToken,
} from '@/lib/data/legacy/verification-tokens';
import { AuthenticatedUser } from '@/lib/data/user-schema';
import { type Adapter, AdapterAccount, VerificationToken } from 'next-auth/adapters';
const Adapter = {
createUser: async (
user: Omit<AuthenticatedUser, 'id'> | { email: string; emailVerified: Date },
) => {
return addUser({
image: null,
...user,
guest: false,
});
},
getUser: async (id: string) => {
return getUserById(id);
},
updateUser: async (user: AuthenticatedUser) => {
return updateUser(user.id, { ...user, guest: false });
},
getUserByEmail: async (email: string) => {
return getUserByEmail(email) ?? null;
},
createVerificationToken: async (token: VerificationToken) => {
return createVerificationToken(token);
},
useVerificationToken: async (params: { identifier: string; token: string }) => {
// next-auth checks if the token is expired
const token = getVerificationToken(params);
if (token) deleteVerificationToken(params);
return token ?? null;
},
linkAccount: async (account: AdapterAccount) => {
return addOauthAccount({
userId: account.userId,
type: account.type,
provider: account.provider,
providerAccountId: account.providerAccountId,
});
},
getUserByAccount: async (account: AdapterAccount) => {
const userAccount = getOauthAccountByProviderId(account.provider, account.providerAccountId);
if (!userAccount) return null;
return getUserById(userAccount.userId) as unknown as AdapterAccount;
},
};
export default Adapter as unknown as Adapter;