Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/oauth/handle error #299

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Post,
Query,
Redirect,
Res,
UseGuards,
} from '@nestjs/common';
import {
Expand All @@ -17,7 +16,6 @@ import {
ApiTags,
} from '@nestjs/swagger';
import type { User } from '@prisma/client';
import { Response } from 'express';
import { CurrentUser } from 'src/common/decorators/current-user.decorator';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
Expand Down Expand Up @@ -64,7 +62,14 @@ export class AuthController {

@Get('signup/oauth2/42/callback')
@ApiOkResponse({ type: AuthEntity })
@Redirect('/login')
@Redirect()
async signupWithOauth42Callback(@Query('code') code: string) {
// only redirect to the frontend with the code in the query
return { url: `/callback/auth/signup/oauth2/42?code=${code}` };
}
lim396 marked this conversation as resolved.
Show resolved Hide resolved

@Get('signup/oauth2/42/authenticate')
@ApiOkResponse({ type: AuthEntity })
async signupWithOauth42(@Query('code') code: string) {
return this.authService.signupWithOauth42(code);
}
Expand All @@ -79,11 +84,16 @@ export class AuthController {

@Get('login/oauth2/42/callback')
@ApiOkResponse({ type: AuthEntity })
loginWithOauth42(@Query('code') code: string, @Res() res: Response) {
return this.authService.loginWithOauth42(code).then((auth) => {
res.cookie('token', auth.accessToken);
res.redirect('/');
});
@Redirect()
loginWithOauth42Callback(@Query('code') code: string) {
// only redirect to the frontend with the code in the query
return { url: `/callback/auth/login/oauth2/42?code=${code}` };
lim396 marked this conversation as resolved.
Show resolved Hide resolved
}

@Get('login/oauth2/42/authenticate')
@ApiOkResponse({ type: AuthEntity })
loginWithOauth42(@Query('code') code: string) {
return this.authService.loginWithOauth42(code);
}

@Post('2fa/generate')
Expand Down
22 changes: 22 additions & 0 deletions frontend/app/callback/auth/login/oauth2/42/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { redirect } from "next/navigation";
import { setAccessToken } from "@/app/lib/session";

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const res = await fetch(
`${process.env.API_URL}/auth/login/oauth2/42/authenticate?${searchParams}`,
{
headers: {
"Content-Type": "application/json",
},
},
);
const data = await res.json();
if (!res.ok) {
console.error("Failed to authenticate", res);
console.error("Failed to authenticate", data);
redirect("/login");
}
setAccessToken(data.accessToken);
redirect("/");
lim396 marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 25 additions & 0 deletions frontend/app/callback/auth/signup/oauth2/42/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { redirect } from "next/navigation";

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
console.error("Route handler called");
console.error(
"fetching",
`${process.env.API_URL}/auth/signup/oauth2/42/authenticate?${searchParams}`,
);
const res = await fetch(
`${process.env.API_URL}/auth/signup/oauth2/42/authenticate?${searchParams}`,
{
headers: {
"Content-Type": "application/json",
},
},
);
const data = await res.json();
if (!res.ok) {
console.error("Failed to authenticate", res);
console.error("Failed to authenticate", data);
redirect("/signup");
}
redirect("/login");
lim396 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading