Skip to content

Commit

Permalink
token checking
Browse files Browse the repository at this point in the history
  • Loading branch information
onlycs committed Jan 13, 2025
1 parent b691bfa commit d818643
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ pub fn get_auth_header(req: &HttpRequest) -> Result<String, RouteError> {
Ok(auth.to_str()?.to_string())
}

#[post("/auth_check")]
async fn auth_check(
req: HttpRequest,
state: web::Data<AppState>,
) -> Result<impl Responder, RouteError> {
authorize(get_auth_header(&req)?, &state.pg).await?;
Ok(HttpResponse::Ok().finish())
}

#[post("/login")]
async fn login(
body: web::Json<AuthRequest>,
Expand Down Expand Up @@ -193,6 +202,7 @@ async fn main() -> Result<(), InitError> {
.service(roster)
.service(csv)
.service(clear)
.service(auth_check)
})
.bind(("0.0.0.0", 8080))?
.run()
Expand Down
18 changes: 15 additions & 3 deletions src/app/attendance/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export default function Attendance() {

const tokencheck = () => {
if (!cookies.get('token')) router.push('/login');

tfetch('/auth_check', { token: cookies.get('token')! })
.then(res => {
if (!res.ok && res.error!.code == 401) {
cookies.remove('token');
router.push('/login');

return;
}
});
};

useEffect(tokencheck);
Expand All @@ -35,6 +45,8 @@ export default function Attendance() {
setError('');
setSuccess('');
theme.setTheme('dark');

tokencheck();
};

const resetSuccess = (msg: string) => {
Expand All @@ -57,9 +69,9 @@ export default function Attendance() {
})
.then(res => {
if (!res.ok) {
resetError(GetError(res.error!.ecode, res.error!.message));
resetError(GetError(res.error!.code, res.error!.message));

if (res.error!.ecode == 401) {
if (res.error!.code == 401) {
cookies.remove('token');
}

Expand All @@ -81,7 +93,7 @@ export default function Attendance() {
})
.then(() => {
if (timeout) clearTimeout(timeout);
setResetTimeout(setTimeout(() => { resetAll(); tokencheck(); }, 1500));
setResetTimeout(setTimeout(() => { resetAll(); }, 1500));
})
.catch(FetchError(resetError));

Expand Down
2 changes: 1 addition & 1 deletion src/app/csv/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function Csv() {
tfetch('/hours.csv', { token: cookies.get('token')! })
.then(res => {
if (!res.ok) {
resetError(GetError(res.error!.ecode, res.error!.message));
resetError(GetError(res.error!.code, res.error!.message));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function Teacher() {
tfetch('/login', { password })
.then(res => {
if (!res.ok) {
setError(GetError(res.error!.ecode, res.error!.message));
setError(GetError(res.error!.code, res.error!.message));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/student/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function Student() {
tfetch('/hours', { id })
.then(res => {
if (!res.ok) {
setError(GetError(res.error!.ecode, res.error!.message));
setError(GetError(res.error!.code, res.error!.message));
return;
}
setHours(res.result!);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/api.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Link from 'next/link';
import { API_URL } from './utils';

interface AuthenticatedRequest {
Expand Down Expand Up @@ -37,7 +36,7 @@ export interface CSVResponse {
}

export interface Error {
ecode: number;
code: number;
message: string;
}

Expand All @@ -46,13 +45,15 @@ export interface Requests {
'/login': LoginRequest;
'/roster': RosterRequest;
'/hours.csv': AuthenticatedRequest;
'/auth_check': AuthenticatedRequest;
}

export interface Responses {
'/hours': HoursResponse;
'/login': LoginResponse;
'/roster': RosterResponse;
'/hours.csv': CSVResponse;
'/auth_check': Record<string, never>;
}

export interface HttpResult<T> {
Expand All @@ -70,7 +71,8 @@ const RequestMethod: Record<Route, 'GET' | 'POST'> = {
'/hours': 'GET',
'/login': 'POST',
'/roster': 'POST',
'/hours.csv': 'GET'
'/hours.csv': 'GET',
'/auth_check': 'POST',
};

export const Errors = {
Expand Down Expand Up @@ -134,7 +136,7 @@ export function tfetch<T extends Route>(route: T, data: Requests[T]): Promise<Ht
resolve({
ok: false,
error: {
ecode: res.status,
code: res.status,
message: await res.text()
}
});
Expand Down

0 comments on commit d818643

Please sign in to comment.