Fixed login bug when one app users the jwt of the other
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
NEXT_PUBLIC_HUB_URL=
|
||||
NEXT_PUBLIC_SERVICE_ID=
|
||||
NEXTAUTH_SECRET=
|
||||
@@ -1,60 +1,68 @@
|
||||
'use client';
|
||||
import { signIn } from 'next-auth/react';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Toaster } from 'react-hot-toast';
|
||||
"use client";
|
||||
import { signIn, useSession } from "next-auth/react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Toaster } from "react-hot-toast";
|
||||
|
||||
export const Login = () => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const searchParams = useSearchParams();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const searchParams = useSearchParams();
|
||||
const { data: session } = useSession();
|
||||
const navigate = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
const signInWithCode = async () => {
|
||||
const code = searchParams.get('code');
|
||||
if (code) {
|
||||
setIsLoading(true);
|
||||
await signIn('credentials', {
|
||||
code: code,
|
||||
callbackUrl: '/',
|
||||
});
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
signInWithCode();
|
||||
}, [searchParams]);
|
||||
useEffect(() => {
|
||||
if (session) {
|
||||
navigate.push("/");
|
||||
}
|
||||
}, [session, navigate]);
|
||||
|
||||
return (
|
||||
<div className="card-body">
|
||||
<div>
|
||||
<Toaster position="top-center" reverseOrder={false} />
|
||||
</div>
|
||||
<h1 className="text-3xl font-bold">Login</h1>
|
||||
<span className="text-sm font-medium">
|
||||
Noch keinen Account? Zur{' '}
|
||||
<a
|
||||
href={`${process.env.NEXT_PUBLIC_HUB_URL}/register`}
|
||||
className="link link-accent link-hover"
|
||||
>
|
||||
Registrierung
|
||||
</a>
|
||||
</span>
|
||||
useEffect(() => {
|
||||
const signInWithCode = async () => {
|
||||
const code = searchParams.get("code");
|
||||
if (code) {
|
||||
setIsLoading(true);
|
||||
await signIn("credentials", {
|
||||
code: code,
|
||||
callbackUrl: "/",
|
||||
});
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
signInWithCode();
|
||||
}, [searchParams]);
|
||||
|
||||
<div className="form-control mt-6">
|
||||
<a
|
||||
href={`${process.env.NEXT_PUBLIC_HUB_URL}/oauth?service=${encodeURIComponent(process.env.NEXT_PUBLIC_SERVICE_ID || '')}&redirect_uri=${encodeURIComponent(`${process.env.NEXT_PUBLIC_PUBLIC_URL}/login`)}`}
|
||||
>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
name="loginBtn"
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading && (
|
||||
<span className="loading loading-spinner loading-sm"></span>
|
||||
)}
|
||||
Login{isLoading && '...'}
|
||||
</button>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
return (
|
||||
<div className="card-body">
|
||||
<div>
|
||||
<Toaster position="top-center" reverseOrder={false} />
|
||||
</div>
|
||||
<h1 className="text-3xl font-bold">Login</h1>
|
||||
<span className="text-sm font-medium">
|
||||
Noch keinen Account? Zur{" "}
|
||||
<a
|
||||
href={`${process.env.NEXT_PUBLIC_HUB_URL}/register`}
|
||||
className="link link-accent link-hover"
|
||||
>
|
||||
Registrierung
|
||||
</a>
|
||||
</span>
|
||||
|
||||
<div className="form-control mt-6">
|
||||
<a
|
||||
href={`${process.env.NEXT_PUBLIC_HUB_URL}/oauth?service=${encodeURIComponent(process.env.NEXT_PUBLIC_SERVICE_ID || "")}&redirect_uri=${encodeURIComponent(`${process.env.NEXT_PUBLIC_PUBLIC_URL}/login`)}`}
|
||||
>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
name="loginBtn"
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading && (
|
||||
<span className="loading loading-spinner loading-sm"></span>
|
||||
)}
|
||||
Login{isLoading && "..."}
|
||||
</button>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,70 +1,82 @@
|
||||
import {
|
||||
AuthOptions,
|
||||
getServerSession as getNextAuthServerSession,
|
||||
} from 'next-auth';
|
||||
import { PrismaAdapter } from '@next-auth/prisma-adapter';
|
||||
import Credentials from 'next-auth/providers/credentials';
|
||||
import { PrismaClient } from '@repo/db';
|
||||
const prisma = new PrismaClient();
|
||||
AuthOptions,
|
||||
getServerSession as getNextAuthServerSession,
|
||||
} from "next-auth";
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { prisma, PrismaClient } from "@repo/db";
|
||||
|
||||
export const options: AuthOptions = {
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {
|
||||
code: { label: 'code', type: 'code' },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
try {
|
||||
if (!credentials) throw new Error('No credentials provided');
|
||||
const code = await prisma.oAuthToken.findFirstOrThrow({
|
||||
where: {
|
||||
accessToken: credentials.code,
|
||||
},
|
||||
});
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: code.userId,
|
||||
},
|
||||
});
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {
|
||||
code: { label: "code", type: "code" },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
try {
|
||||
if (!credentials) throw new Error("No credentials provided");
|
||||
const code = await prisma.oAuthToken.findFirstOrThrow({
|
||||
where: {
|
||||
accessToken: credentials.code,
|
||||
},
|
||||
});
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: code.userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) return null;
|
||||
if (!user) return null;
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
maxAge: 30 * 24 * 60 * 60,
|
||||
},
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
cookies: {
|
||||
sessionToken: {
|
||||
name: `next-auth.session-token-${process.env.NEXTAUTH_URL}`,
|
||||
options: {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
adapter: PrismaAdapter(prisma as any),
|
||||
callbacks: {
|
||||
jwt: async ({ token, user }) => {
|
||||
if (user && 'firstname' in user) {
|
||||
return {
|
||||
...token,
|
||||
...user,
|
||||
};
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session: async ({ session, user, token }) => {
|
||||
return {
|
||||
...session,
|
||||
user: token,
|
||||
};
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: '/login',
|
||||
signOut: '/logout',
|
||||
error: '/authError',
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
maxAge: 30 * 24 * 60 * 60,
|
||||
},
|
||||
|
||||
adapter: PrismaAdapter(prisma as any),
|
||||
callbacks: {
|
||||
jwt: async ({ token, user }) => {
|
||||
if (user && "firstname" in user) {
|
||||
return {
|
||||
...token,
|
||||
...user,
|
||||
};
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session: async ({ session, user, token }) => {
|
||||
return {
|
||||
...session,
|
||||
user: token,
|
||||
};
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
signOut: "/logout",
|
||||
error: "/authError",
|
||||
},
|
||||
} satisfies AuthOptions;
|
||||
|
||||
export const getServerSession = async () => getNextAuthServerSession(options);
|
||||
|
||||
Reference in New Issue
Block a user