-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend] * Create an API Route on the frontend side that receives oauth(signup) callbacks and calls the backend API * Create an API Route on the frontend side that receives oauth(login) callbacks and calls the backend API * Add query to redirect to /signup or /login in case of OAuth failure * Added toast display when using useSearchParams to get error status and message [backend] * Create function that only redirects to frontend with authorization code
- Loading branch information
Showing
5 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { setAccessToken } from "@/app/lib/session"; | ||
import { redirect } from "next/navigation"; | ||
|
||
const isValidUrl = (url: string) => { | ||
return URL.canParse(url); | ||
}; | ||
|
||
export async function GET(request: Request) { | ||
if (!isValidUrl(request.url)) { | ||
redirect("/login"); | ||
} | ||
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", data); | ||
const params = new URLSearchParams({ | ||
status: data.statusCode, | ||
message: data.message, | ||
}); | ||
const query = "?" + params.toString(); | ||
redirect("/login" + query); | ||
} else { | ||
setAccessToken(data.accessToken); | ||
redirect("/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { redirect } from "next/navigation"; | ||
|
||
const isValidUrl = (url: string) => { | ||
return URL.canParse(url); | ||
}; | ||
|
||
export async function GET(request: Request) { | ||
if (!isValidUrl(request.url)) { | ||
redirect("/signup"); | ||
} | ||
const { searchParams } = new URL(request.url); | ||
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", data); | ||
const params = new URLSearchParams({ | ||
status: data.statusCode, | ||
message: data.message, | ||
}); | ||
const query = "?" + params.toString(); | ||
redirect("/signup" + query); | ||
} else { | ||
redirect("/login"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters