import { client } from 'lib/apollo_graphql/client' ; import { getAuthToken } from 'lib/apollo_graphql/mutations/getAuthToken' ; import { action , computed , configure , observable } from 'mobx' ; import ProfileStore from './ProfileStore' ; import _ from 'lodash' ; configure ( { enforceActions : 'observed' } ) ; declare var GITHUB_CLIENT_ID ; declare var GOOGLE_CLIENT_ID ; declare var FACEBOOK_CLIENT_ID ; declare var NAVER_CLIENT_ID ; export enum clientIdEnum { github = GITHUB_CLIENT_ID , google = GOOGLE_CLIENT_ID , facebook = FACEBOOK_CLIENT_ID , naver = NAVER_CLIENT_ID } const TOKEN_KEY = 'token' ; const CODE = 'code' ; export class AuthStore { @ observable isInitialized = false ; @ observable inProgress : boolean = false ; @ observable state : string = 'pending' ; @ observable oAuthType ? : keyof typeof clientIdEnum ; @ observable clientId ? : clientIdEnum ; @ observable accessToken ? : string | null = null ; @ observable code ? : string | null = null ; @ observable language ? : string | null = null ; @ action async login ( oAuthType , code , redirect_url ) { if ( ! oAuthType || ! code ) { return ; } this . oAuthType = oAuthType ; this . clientId = clientIdEnum [ this . oAuthType ] ; const localStorageCode = localStorage . getItem ( CODE ) ; const localStorageAccessCode = localStorage . getItem ( TOKEN_KEY ) ; if ( ! _ . isEqual ( localStorageCode , code ) && ! localStorageAccessCode ) { const response = await getAuthToken ( client ) ( { clientId : this . clientId , oauthType : this . oAuthType , code , redirectUri : redirect_url } ) ; const accessToken = response . data . oAuthTokenAuth . token ; localStorage . setItem ( TOKEN_KEY , accessToken ) ; localStorage . setItem ( CODE , code ) ; } this . setAccessToken ( localStorage . getItem ( TOKEN_KEY ) ) ; this . setCode ( localStorage . getItem ( CODE ) ) ; this . isInitialized = true ; return ProfileStore . retrieveMe ( ) ; } @ action logout ( ) { this . accessToken = null ; ProfileStore . clearUser ( ) ; localStorage . removeItem ( TOKEN_KEY ) ; localStorage . removeItem ( CODE ) ; } @ action setCode ( code ) { this . code = code ; } @ action setAccessToken ( accessToken ) { this . accessToken = accessToken ; } @ computed get loggedIn ( ) { return this . accessToken != null ; } @ action syncToken ( ) { this . accessToken = localStorage . getItem ( TOKEN_KEY ) ; this . isInitialized = true ; } @ action setLanguage ( language ) { this . language = language ; } } export default new AuthStore ( ) ;