84 lines
1.8 KiB
TypeScript
84 lines
1.8 KiB
TypeScript
import { api } from "./api";
|
|
|
|
export const TOKEN_KEY = "@smartAuth-token";
|
|
|
|
export const logout = () => {
|
|
localStorage.removeItem(TOKEN_KEY);
|
|
};
|
|
|
|
interface SignInRequestData {
|
|
email: string,
|
|
password: string
|
|
}
|
|
|
|
type UserObjectType = {
|
|
name: string;
|
|
email: string;
|
|
client_id: number;
|
|
id: number;
|
|
role: number;
|
|
profile_picture?: string
|
|
}
|
|
|
|
export async function signInRequest(data: SignInRequestData) {
|
|
let user: UserObjectType, token: string, exception: any = null
|
|
|
|
await api.post('/auth/login', {
|
|
"email": data.email,
|
|
"password": data.password,
|
|
"device_name": "test"
|
|
}).then(res => {
|
|
token = res.data.token
|
|
user = {
|
|
name: res.data.user.name,
|
|
email: res.data.user.email,
|
|
client_id: res.data.user.client_id,
|
|
id: res.data.user.id,
|
|
role: res.data.user.roles[0].pivot.role_id,
|
|
profile_picture: res.data.user.profile_picture
|
|
}
|
|
}).catch(res => {
|
|
exception = res
|
|
})
|
|
|
|
return {
|
|
token,
|
|
user: {
|
|
name: user?.name,
|
|
email: user?.email,
|
|
client_id: user?.client_id,
|
|
id: user?.id,
|
|
role: user?.role,
|
|
profile_picture: user?.profile_picture
|
|
},
|
|
exception
|
|
}
|
|
}
|
|
|
|
export default async function recoverUserInformation(id) {
|
|
let user: UserObjectType
|
|
|
|
await api.get(`/user/${id}`).then(res => {
|
|
user = {
|
|
name: res.data.user.name,
|
|
email: res.data.user.email,
|
|
client_id: res.data.user.client_id,
|
|
id: res.data.user.id,
|
|
role: res.data.user.roles[0].pivot.role_id,
|
|
profile_picture: res.data.user.profile_picture
|
|
}
|
|
}).catch(res => {
|
|
// console.log(res)
|
|
})
|
|
|
|
return {
|
|
user: {
|
|
name: user?.name,
|
|
email: user?.email,
|
|
client_id: user?.client_id,
|
|
id: user?.id,
|
|
profile_picture: user?.profile_picture
|
|
}
|
|
}
|
|
}
|