-
Notifications
You must be signed in to change notification settings - Fork 17
/
select-auth.ts
45 lines (38 loc) · 1.42 KB
/
select-auth.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
import { AuthenticateOptions } from "passport";
import { RequestHandler } from "express-serve-static-core";
import { authenticateFunction } from "../services/auth/auth-lib";
import Config from "../config";
const googleOptions: AuthenticateOptions = {
session: false,
scope: ["profile", "email"],
};
const githubOptions: AuthenticateOptions = { session: false };
type CustomOptions = AuthenticateOptions & {
callbackURL: string;
};
/**
* Given a provider, return the middleware function corresponding to the handler
* @public
* @param provider String representing the provider passed into the request
* @param device String representing the device that auth is being performed on
* @returns RequestHandler middleware, that's pre-configured for the provider
*/
export function SelectAuthProvider(provider: string, device: string): RequestHandler {
if (provider == "google") {
const options: CustomOptions = {
...googleOptions,
callbackURL: Config.CALLBACK_URLS.GOOGLE,
};
options.callbackURL += device;
return authenticateFunction("google", options);
}
if (provider == "github") {
const options: CustomOptions = {
...githubOptions,
callbackURL: Config.CALLBACK_URLS.GITHUB,
};
options.callbackURL += device;
return authenticateFunction("github", options);
}
throw new Error("Provider not found!");
}