how do you handle successful login in oauth?
Answered
American black bear posted this in #help-forum
American black bearOP
i cannot figure out how to handle a successful login after redirect in an oauth login system,
Answered by American black bear
my solution:
export async function GET(req: NextRequest) {
const param= req.nextUrl.searchParams.get("param") as string}
export async function GET(req: NextRequest) {
const param= req.nextUrl.searchParams.get("param") as string}
30 Replies
Masai Lion
code?
and a bit more of info pls
American black bearOP
yeah i just created the post, let me explain real quick
how do i handle the redirect after a successful login in a oauth system? for instance, the user, after logging in, gets redirected to a redirect uri i set up with the access token in the url. my question is how do i extract that access token from the url for later use and redirect the user to the url i want?
@American black bear how do i handle the redirect after a successful login in a oauth system? for instance, the user, after logging in, gets redirected to a redirect uri i set up with the access token in the url. my question is how do i extract that access token from the url for later use and redirect the user to the url i want?
Masai Lion
Mmm you can handle the redirect after a successful login in an OAuth system by utilizing Next.js routing and the useRouter hook provided by Next.js.
Something like this:
import { useRouter } from 'next/router';
import { useEffect } from 'react';
function LoginPage() {
const router = useRouter();
useEffect(() => {
// Extract access token from the URL query parameters
const { access_token } = router.query;
if (access_token) {
// Store the access token for later use (like in local storage)
localStorage.setItem('accessToken', access_token);
// Then redirect
router.push('/dashboard');
}
}, [router.query]);
return (
<div>
{/* Your form */}
</div>
);
}
export default LoginPage;LoginPage is our component where the user is redirected after successful login. Inside the useEffect hook, we extract the access_token from the query parameters using router.query. If an access token is found, we store it in local storage and redirect the user to the /dashboard page using router.push('/dashboard') or whatever your directory is
@Masai Lion js
import { useRouter } from 'next/router';
import { useEffect } from 'react';
function LoginPage() {
const router = useRouter();
useEffect(() => {
// Extract access token from the URL query parameters
const { access_token } = router.query;
if (access_token) {
// Store the access token for later use (like in local storage)
localStorage.setItem('accessToken', access_token);
// Then redirect
router.push('/dashboard');
}
}, [router.query]);
return (
<div>
{/* Your form */}
</div>
);
}
export default LoginPage;
American black bearOP
shouldn't i use useSearchParams() instead of router.query? im using app router
Masai Lion
Oh
Yeah
Sorry
American black bearOP
and i would need to run this whole thing on client right?
Masai Lion
As far as I understand then you would typically run this code on the client side because it involves interacting with the browser's URL and localStorage, which are client-side functionalities.
import { useRouter } from 'next/router';
import { useEffect } from 'react';
function LoginPage() {
const router = useRouter();
useEffect(() => {
const queryParams = new URLSearchParams(router.asPath.split('?')[1]);
const accessToken = queryParams.get('access_token');
if (accessToken) {
localStorage.setItem('accessToken', accessToken);
router.push('/dashboard');
}
}, []);
return (
<div>
{/* Login form goes here */}
</div>
);
}
export default LoginPage;@Masai Lion As far as I understand then you would typically run this code on the client side because it involves interacting with the browser's URL and localStorage, which are client-side functionalities.
American black bearOP
yeah and also cause useEffect is client side so i would need to run the whole thing on clietn anyways
thank you very much
Masai Lion
This would be then the maybe correct snippet
@American black bear thank you very much
Masai Lion
Sure!
No problem
@American black bear yeah and also cause useEffect is client side so i would need to run the whole thing on clietn anyways
Masai Lion
Yeah use add a “use client†above all your code
Masai Lion
@American black bear if the problem is solved please mark it as solved or if issues persist please let me know and ignore everything above lol ðŸ˜
American black bearOP
yeah im trying right now, will mark as solved as soon as it will be :)
@American black bear yeah im trying right now, will mark as solved as soon as it will be :)
Masai Lion
Hello! Everything good?
@Masai Lion Hello! Everything good?
American black bearOP
no i solved it in a much simpler way, will send solution as soon as im home
@American black bear no i solved it in a much simpler way, will send solution as soon as im home
Masai Lion
Oh! Good to hear! Sure.
@American black bear no i solved it in a much simpler way, will send solution as soon as im home
Masai Lion
Hello! Can you mark this as solved!?
American black bearOP
my solution:
export async function GET(req: NextRequest) {
const param= req.nextUrl.searchParams.get("param") as string}
export async function GET(req: NextRequest) {
const param= req.nextUrl.searchParams.get("param") as string}
Answer