fix login
This commit is contained in:
parent
481b78ea41
commit
e82bf612f9
@ -31,22 +31,35 @@ export function AuthProvider({children}: {children: React.ReactNode}) {
|
|||||||
const isAuthenticated = !!user
|
const isAuthenticated = !!user
|
||||||
|
|
||||||
async function signIn({email, password}: SignInData) {
|
async function signIn({email, password}: SignInData) {
|
||||||
const { token, user }: any = await signInRequest({
|
const { token, user, exception }: any = await signInRequest({
|
||||||
email,
|
email,
|
||||||
password
|
password
|
||||||
})
|
})
|
||||||
|
|
||||||
setCookie(undefined, '@smartAuth-token', token, {
|
if (token) {
|
||||||
maxAge: 60 * 60 * 1, // 1 hour
|
setCookie(undefined, '@smartAuth-token', token, {
|
||||||
})
|
maxAge: 60 * 60 * 1, // 1 hour
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
setCookie(undefined, 'user-role', user.role)
|
if (user.role) {
|
||||||
|
setCookie(undefined, 'user-role', user.role)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exception) {
|
||||||
|
if (user.role == 2) {
|
||||||
|
Router.push('/dashboard')
|
||||||
|
} else {
|
||||||
|
Router.push('administrative/clients')
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
api.defaults.headers['Authorization'] = `Bearer ${token}`
|
api.defaults.headers['Authorization'] = `Bearer ${token}`
|
||||||
|
|
||||||
setUser(user)
|
setUser(user)
|
||||||
|
|
||||||
Router.push('/dashboard')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -66,7 +66,6 @@ export default function Dashboard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||||
const apiClient = getAPIClient(ctx)
|
|
||||||
const { ['@smartAuth-token']: token } = parseCookies(ctx)
|
const { ['@smartAuth-token']: token } = parseCookies(ctx)
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
|
|||||||
@ -20,14 +20,14 @@ type UserObjectType = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function signInRequest(data: SignInRequestData) {
|
export async function signInRequest(data: SignInRequestData) {
|
||||||
let user: UserObjectType, token: string
|
let user: UserObjectType, token: string, exception: any = null
|
||||||
|
|
||||||
await api.post('/auth/login', {
|
await api.post('/auth/login', {
|
||||||
"email": data.email,
|
"email": data.email,
|
||||||
"password": data.password,
|
"password": data.password,
|
||||||
"device_name": "test"
|
"device_name": "test"
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
// console.log(res.data.user.roles.pivot.role_id)
|
token = res.data.token
|
||||||
user = {
|
user = {
|
||||||
name: res.data.user.name,
|
name: res.data.user.name,
|
||||||
email: res.data.user.email,
|
email: res.data.user.email,
|
||||||
@ -35,20 +35,20 @@ export async function signInRequest(data: SignInRequestData) {
|
|||||||
id: res.data.user.id,
|
id: res.data.user.id,
|
||||||
role: res.data.user.roles[0].pivot.role_id
|
role: res.data.user.roles[0].pivot.role_id
|
||||||
}
|
}
|
||||||
token = res.data.token
|
|
||||||
}).catch(res => {
|
}).catch(res => {
|
||||||
console.log(res)
|
exception = res
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
token: token,
|
token,
|
||||||
user: {
|
user: {
|
||||||
name: user?.name,
|
name: user?.name,
|
||||||
email: user?.email,
|
email: user?.email,
|
||||||
client_id: user?.client_id,
|
client_id: user?.client_id,
|
||||||
id: user?.id,
|
id: user?.id,
|
||||||
role: user?.role
|
role: user?.role
|
||||||
}
|
},
|
||||||
|
exception
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user