Skip to content
Permalink
v1.8.30
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
82 lines (68 sloc) 1.98 KB
import { emitEvent, currentRoute, navigateToRoute, storeItem } from "@factor/api"
import { userRolesMap, CurrentUserState, UserRoles } from "./types"
import { userToken } from "./token"
let __initializedUser: Promise<CurrentUserState> | CurrentUserState
export const initializedUser = (
action?: "set" | "get",
value?: Promise<CurrentUserState> | CurrentUserState
): Promise<CurrentUserState> | CurrentUserState => {
if (action == "set") {
__initializedUser = value
}
return __initializedUser
}
export const roleAccessLevel = (role: UserRoles | undefined): number => {
return role && userRolesMap[role] ? userRolesMap[role] : 0
}
export interface SetUser {
user: CurrentUserState
token?: string
current?: boolean
}
/**
* Set persistent user info
*/
export const setUser = ({ user, token = "", current = false }: SetUser): void => {
if (current) {
initializedUser("set", user ? user : undefined)
if (token && user) userToken(token)
else if (user === undefined) userToken("destroy")
storeItem("currentUser", user)
// In certain environments (testing) and with high privacy settings, localStorage is unset
if (localStorage) {
localStorage[user ? "setItem" : "removeItem"]("user", JSON.stringify(user))
}
}
if (user && user._id) storeItem(user._id, user)
}
/**
* Shows sign in
*/
export const showSignIn = (
options: {
redirect?: string
view?: string
mode?: "page" | "modal"
} = {}
): void => {
emitEvent("signin-form", options)
}
/**
* Logs out the current user
*/
export const logout = async (args: { redirect?: string } = {}): Promise<void> => {
setUser({ user: undefined, current: true })
emitEvent("logout")
emitEvent("notify", "Successfully logged out.")
const theCurrentRoute = currentRoute()
if (
args.redirect ||
!theCurrentRoute ||
theCurrentRoute.matched.some((r) => r.meta.auth)
) {
const { redirect: path = "/" } = args
navigateToRoute({ path })
} else {
emitEvent("reset-ui")
}
}