mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 10:34:56 -06:00
52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
//@flow
|
|
import { authFacebook, type FacebookAuth } from './apis/FacebookAuth';
|
|
import moment from 'moment';
|
|
|
|
export const AUTH_PROVIDER_FACEBOOK: 'facebook' = 'facebook';
|
|
|
|
export type AUTH_PROVIDER = typeof AUTH_PROVIDER_FACEBOOK;
|
|
|
|
export type User = {
|
|
name: string,
|
|
token: string,
|
|
tokenExpires: number,
|
|
email: string,
|
|
photo: string,
|
|
provider: AUTH_PROVIDER,
|
|
};
|
|
|
|
class AuthManager {
|
|
user: ?User;
|
|
|
|
getToken = () => {
|
|
return this.user && this.user.token;
|
|
};
|
|
|
|
isLoggedIn = () => {
|
|
if (!this.user) {
|
|
return false;
|
|
}
|
|
const { token, tokenExpires } = this.user;
|
|
return token && +moment(tokenExpires) >= +moment();
|
|
};
|
|
|
|
setFacebookUser = ({ credentials, profile }: FacebookAuth) => {
|
|
this.user = {
|
|
token: credentials.token,
|
|
tokenExpires: +moment(credentials.tokenExpirationDate),
|
|
name: profile.name,
|
|
email: profile.email,
|
|
photo: profile.picture.data.url,
|
|
provider: AUTH_PROVIDER_FACEBOOK,
|
|
};
|
|
};
|
|
|
|
authorize = async (provider: AUTH_PROVIDER) => {
|
|
switch (provider) {
|
|
case AUTH_PROVIDER_FACEBOOK:
|
|
this.setFacebookUser(await authFacebook());
|
|
}
|
|
};
|
|
}
|
|
|
|
export default new AuthManager();
|