Fixed refresh missing email issue
This commit is contained in:
@@ -152,7 +152,7 @@ impl FromRequest for JwtAuth {
|
|||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return ready(Err(ActixError::from(ServiceError {
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 404,
|
status: 401,
|
||||||
message: format!("Access token was not found")
|
message: format!("Access token was not found")
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,7 @@ impl FromRequest for JwtAuth {
|
|||||||
ready(Ok(JwtAuth { token: access_token_uuid, user: user.into() }))
|
ready(Ok(JwtAuth { token: access_token_uuid, user: user.into() }))
|
||||||
}
|
}
|
||||||
Err(_) => return ready(Err(ActixError::from(ServiceError {
|
Err(_) => return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 404,
|
status: 401,
|
||||||
message: format!("User was not found")
|
message: format!("User was not found")
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,22 +155,7 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut conn = match db::redis_async_connection().await {
|
let email = refresh_token_details.email.clone();
|
||||||
Ok(conn) => conn,
|
|
||||||
Err(err) => {
|
|
||||||
error!("Failed to get redis connection: {}", err);
|
|
||||||
return ResponseError::error_response(&err)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let redis_result: redis::RedisResult<String> = conn.get(refresh_token_details.token_uuid.to_string()).await;
|
|
||||||
let email = match redis_result {
|
|
||||||
Ok(email) => email,
|
|
||||||
Err(_) => return ResponseError::error_response(&ServiceError {
|
|
||||||
status: 404,
|
|
||||||
message: format!("Refresh token was not found")
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
match QueryUser::get_by_email(&email) {
|
match QueryUser::get_by_email(&email) {
|
||||||
Ok(query_user) => {
|
Ok(query_user) => {
|
||||||
@@ -182,6 +167,14 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut conn = match db::redis_async_connection().await {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(err) => {
|
||||||
|
error!("Failed to get redis connection: {}", err);
|
||||||
|
return ResponseError::error_response(&err)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Delete old auth token if it exists
|
// Delete old auth token if it exists
|
||||||
match req.cookie("access_token") {
|
match req.cookie("access_token") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { getRequest, postRequest } from '.';
|
import { getRequest, postRequest } from '.';
|
||||||
import { RegisterUser, ResponseUser } from './auth.types';
|
import { RegisterUser, ResponseAuth } from './auth.types';
|
||||||
|
|
||||||
export async function login(email: string, password: string): Promise<ResponseUser | undefined> {
|
export async function login(email: string, password: string): Promise<ResponseAuth | undefined> {
|
||||||
const response = await postRequest('auth/login', { email, password }, { withCredentials: true });
|
const response = await postRequest('auth/login', { email, password }, { withCredentials: true });
|
||||||
if (response?.status === 200) {
|
if (response?.status === 200) {
|
||||||
return response.data as ResponseUser;
|
return response.data as ResponseAuth;
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@@ -23,16 +23,16 @@ export async function logout() {
|
|||||||
return await postRequest('auth/logout', {}, { withCredentials: true });
|
return await postRequest('auth/logout', {}, { withCredentials: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function refresh(refresh_token_rotation?: boolean): Promise<ResponseUser | undefined> {
|
export async function refresh(refresh_token_rotation?: boolean): Promise<ResponseAuth | undefined> {
|
||||||
const response = await getRequest('auth/refresh', { withCredentials: true, params: { refresh_token_rotation } });
|
const response = await getRequest('auth/refresh', { withCredentials: true, params: { refresh_token_rotation } });
|
||||||
if (response?.status === 200) {
|
if (response?.status === 200) {
|
||||||
return response.data as ResponseUser;
|
return response.data as ResponseAuth;
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function me(): Promise<ResponseUser | undefined> {
|
export async function me(): Promise<ResponseAuth | undefined> {
|
||||||
const response = await getRequest('auth/me', { withCredentials: true });
|
const response = await getRequest('auth/me', { withCredentials: true });
|
||||||
if (response?.status === 200) {
|
if (response?.status === 200) {
|
||||||
return response.data;
|
return response.data;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export interface ResponseUser {
|
export interface ResponseAuth {
|
||||||
token: string;
|
token: string;
|
||||||
user: User;
|
user: User;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useForm } from '@mantine/form';
|
import { useForm } from '@mantine/form';
|
||||||
import { login, register, logout, me } from '@/api/auth';
|
import { login, register, logout, me, refresh } from '@/api/auth';
|
||||||
import { User } from '@/api/auth.types';
|
import { User } from '@/api/auth.types';
|
||||||
import { useToggle } from '@mantine/hooks';
|
import { useToggle } from '@mantine/hooks';
|
||||||
|
|
||||||
@@ -82,7 +82,13 @@ export default function Topbar() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setUser(undefined);
|
refresh(true).then((response) => {
|
||||||
|
if (response) {
|
||||||
|
setUser(response.user);
|
||||||
|
} else {
|
||||||
|
setUser(undefined);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user