-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.server.ts
49 lines (38 loc) · 1.42 KB
/
auth.server.ts
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
import { Authenticator } from 'remix-auth';
import { FormStrategy } from 'remix-auth-form';
import { GitHubStrategy } from 'remix-auth-github';
import type { UserSession } from '@types';
import { sessionStorage } from './session.server';
import { findByCredentials, findOrCreateByProfile } from '~/models/user.server';
const authenticator = new Authenticator<UserSession | null>(sessionStorage);
// If using the GitHub strategy, it is recommended to validate the ENV vars exist
// invariant(process.env.GITHUB_CLIENT_ID, 'GITHUB_CLIENT_ID must be set');
// invariant(process.env.GITHUB_CLIENT_SECRET, 'GITHUB_CLIENT_SECRET must be set');
// invariant(process.env.GITHUB_CALLBACK_URL, 'GITHUB_CALLBACK_URL must be set');
authenticator.use(
new FormStrategy(async ({ form }) => {
const email = form.get('email')?.toString();
const password = form.get('password')?.toString();
if (!email || !password) {
return null;
}
const user = await findByCredentials(email, password);
return user;
}),
'credentials',
);
// Example use of an OAuth strategy
authenticator.use(
new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID ?? '',
clientSecret: process.env.GITHUB_CLIENT_SECRET ?? '',
callbackURL: process.env.GITHUB_CALLBACK_URL ?? '',
},
async ({ profile }) => {
const user = await findOrCreateByProfile(profile);
return user;
},
),
);
export { authenticator };