-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
Copy pathgitHubAuth.server.ts
55 lines (48 loc) · 1.46 KB
/
gitHubAuth.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
50
51
52
53
54
55
import type { Authenticator } from "remix-auth";
import { GitHubStrategy } from "remix-auth-github";
import { env } from "~/env.server";
import { findOrCreateUser } from "~/models/user.server";
import type { AuthUser } from "./authUser";
import { postAuthentication } from "./postAuth.server";
import { logger } from "./logger.server";
import { normalizeEmail } from "~/utils/email";
export function addGitHubStrategy(
authenticator: Authenticator<AuthUser>,
clientID: string,
clientSecret: string
) {
const gitHubStrategy = new GitHubStrategy(
{
clientID,
clientSecret,
callbackURL: `${env.LOGIN_ORIGIN}/auth/github/callback`,
},
async ({ extraParams, profile }) => {
const emails = profile.emails;
if (!emails) {
throw new Error("GitHub login requires an email address");
}
try {
logger.debug("GitHub login", {
emails,
profile,
extraParams,
});
const { user, isNewUser } = await findOrCreateUser({
email: normalizeEmail(emails[0].value),
authenticationMethod: "GITHUB",
authenticationProfile: profile,
authenticationExtraParams: extraParams,
});
await postAuthentication({ user, isNewUser, loginMethod: "GITHUB" });
return {
userId: user.id,
};
} catch (error) {
console.error(error);
throw error;
}
}
);
authenticator.use(gitHubStrategy);
}