|
|
import hashPii from './hash-pii'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let _currentUser: CurrentUser; |
|
|
|
|
|
export interface SetCurrentUserParams { |
|
|
ID: string; |
|
|
username: string; |
|
|
email: string; |
|
|
localeSlug: string; |
|
|
} |
|
|
|
|
|
export interface CurrentUserHashedPii { |
|
|
ID: string; |
|
|
username: string; |
|
|
email: string; |
|
|
} |
|
|
|
|
|
export interface CurrentUser { |
|
|
ID: number; |
|
|
username: string; |
|
|
email: string; |
|
|
hashedPii: CurrentUserHashedPii; |
|
|
localeSlug: string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getCurrentUser(): CurrentUser { |
|
|
return _currentUser; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setCurrentUser( currentUser: SetCurrentUserParams ): CurrentUser | undefined { |
|
|
if ( |
|
|
! currentUser.ID || |
|
|
isNaN( parseInt( currentUser.ID, 10 ) ) || |
|
|
! currentUser.username || |
|
|
! currentUser.email |
|
|
) { |
|
|
return; |
|
|
} |
|
|
_currentUser = { |
|
|
ID: parseInt( currentUser.ID, 10 ), |
|
|
username: currentUser.username, |
|
|
email: currentUser.email, |
|
|
localeSlug: currentUser.localeSlug, |
|
|
hashedPii: { |
|
|
ID: hashPii( currentUser.ID ), |
|
|
username: hashPii( currentUser.username.toLowerCase().replace( /\s/g, '' ) ), |
|
|
email: hashPii( currentUser.email.toLowerCase().replace( /\s/g, '' ) ), |
|
|
}, |
|
|
}; |
|
|
return _currentUser; |
|
|
} |
|
|
|