-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
oidc-providers.ts
97 lines (90 loc) · 3.24 KB
/
oidc-providers.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-disable no-async-promise-executor */
import {UserManager, UserManagerSettings} from 'oidc-client-ts';
import {sleep} from './helpers';
declare const FB: any;
const GOOGLE_CONFIG: UserManagerSettings = {
authority: 'https://accounts.google.com',
client_id:
'533830427279-cspigijdu0g50c7imca5pvdbrcn2buaq.apps.googleusercontent.com',
client_secret: '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',
redirect_uri: `${window.location.protocol}//${window.location.host}/callback`,
scope: 'openid email profile',
loadUserInfo: true
};
export const GoogleProvider = new UserManager(GOOGLE_CONFIG);
export const facebookLogin = () => {
return new Promise((res, rej) => {
let authResponse: any;
FB.login(
(r: any) => {
if (r.authResponse) {
authResponse = r.authResponse;
FB.api(
'/me?fields=id,name,email,picture.width(640).height(640)',
(profileResponse: any) => {
authResponse.profile = profileResponse;
authResponse.profile.picture =
profileResponse.picture.data.url;
res(authResponse);
}
);
} else {
console.log(
'User cancelled login or did not fully authorize.'
);
rej(undefined);
}
},
{scope: 'public_profile,email'}
);
});
};
export const getFacebookLoginStatus = () => {
return new Promise((res) => {
let authResponse: any = {};
FB.getLoginStatus((r: any) => {
if (r.authResponse) {
authResponse = r.authResponse;
FB.api(
'/me?fields=id,name,email,picture.width(640).height(640)',
(profileResponse: any) => {
authResponse.profile = profileResponse;
authResponse.profile.picture =
profileResponse.picture.data.url;
res(authResponse);
}
);
} else {
res(undefined);
}
});
});
};
export const authLogin = (email: string, password: string) => {
return new Promise(async (res, rej) => {
await sleep(500);
if (email === 'admin@example.com' && password === 'admin') {
localStorage.setItem(
'authentication',
JSON.stringify({profile: {email: 'admin@example.com'}})
);
return res({profile: {email: 'admin@example.com'}});
}
return rej({message: 'Credentials are wrong!'});
});
};
export const getAuthStatus = () => {
return new Promise(async (res) => {
await sleep(500);
try {
let authentication = localStorage.getItem('authentication');
if (authentication) {
authentication = JSON.parse(authentication);
return res(authentication);
}
return res(undefined);
} catch (error) {
return res(undefined);
}
});
};