22import axios from "axios" ;
33import { Credentials } from "../app/account/login/page" ;
44import userApi from "@api/userApi" ;
5+
56export interface User {
6- id : number ;
7- username : string ;
8- refreshToken : string ;
9- profileImage ?: string | null ;
7+ id : number ;
8+ username : string ;
9+ refreshToken : string ;
10+ profileImage ?: string | null ;
1011}
1112
1213const SIGNIN_URL = process . env . NEXT_PUBLIC_SERVER_URL + "/user/signin" ;
@@ -35,6 +36,10 @@ class AuthService {
3536 }
3637
3738 public getUser ( ) : User | null {
39+ if ( typeof window === "undefined" ) {
40+ return null ;
41+ }
42+
3843 if ( this . user ) {
3944 return this . user ;
4045 }
@@ -46,30 +51,21 @@ class AuthService {
4651 this . user = user ;
4752 }
4853
49- // Gets the user info implicitly using the cookie
54+ // Gets the user info implicitly using the cookie
5055 // and sets the user info.
51- public async getUserInfoOauth2 ( ) : Promise < User | null > {
52- const backendUser = ( await userApi . userInfo ( ) ) as any ; // the response from backend
53-
54- if ( backendUser ) {
55- const user : User = {
56- id : backendUser . userId ,
57- username : backendUser . username ,
58- refreshToken : "" ,
59- profileImage : backendUser . userPhoto || null ,
60- } ;
61-
62- this . setUser ( user ) ;
63- localStorage . setItem ( "user" , JSON . stringify ( user ) ) ;
64- this . authorizedState = AuthStatus . Authorized ;
65- this . notify ( this . authorizedState ) ;
66-
67- return user ;
68- }
69- return null ;
56+ public async getUserInfoOauth2 ( ) : Promise < User | null > {
57+ let cookieuser = ( await userApi . userInfo ( ) ) as unknown as User ;
58+ if ( cookieuser ) {
59+ this . setUser ( cookieuser ) ;
7060 }
61+ localStorage . setItem ( "user" , JSON . stringify ( cookieuser ) ) ;
62+ this . authorizedState = AuthStatus . Authorized ;
63+ this . notify ( this . authorizedState ) ;
64+
65+ return cookieuser ;
66+ }
7167
72- public getAuthorized ( ) : AuthStatus {
68+ public getAuthorized ( ) : AuthStatus {
7369 return this . getUser ( ) ? AuthStatus . Authorized : AuthStatus . Unauthorized ;
7470 }
7571
@@ -86,20 +82,20 @@ class AuthService {
8682 } )
8783 . then ( ( response ) => {
8884 if ( response . status == 200 ) {
89- let signedInUser : User = {
90- id : response . data . id ,
91- username : credentials . username ,
92- refreshToken : response . data . refreshToken ,
93- profileImage : response . data . profileImage || null ,
94- } ;
95- localStorage . setItem ( "user" , JSON . stringify ( signedInUser ) ) ;
96- this . user = signedInUser ;
85+ let signedinUser : User = {
86+ id : response . data . id ,
87+ username : credentials . username ,
88+ refreshToken : response . data . refreshToken ,
89+ profileImage : response . data . profileImage ,
90+ } ;
91+ localStorage . setItem ( "user" , JSON . stringify ( signedinUser ) ) ;
92+ this . user = signedinUser ;
9793 this . authorizedState = AuthStatus . Authorized ;
9894 this . notify ( this . authorizedState ) ;
9995 success = true ;
10096 }
10197 } )
102- . catch ( ( ) => { } ) ;
98+ . catch ( ( ) => { } ) ;
10399 return success ;
104100 }
105101
0 commit comments