keystone-nextjs-auth
keystone-nextjs-auth copied to clipboard
Is there a sample for Credentials?
Hi, I tried to use CredentialsProvider but keep getting access denied when logging in. I then realised that /api/auth/callback/credentials is not supported (I get this error when i visit the url Error: Callback for provider type credentials not supported). Thanks
Edit: I tried with google provider and it still doesn't work. It seems to be an issue with the isAccessAllowed function internally in this package. The context.session is undefined even if i am logged in
Hi @Jun-Xiang unfortunately Credentials are not currently supported by this package, it would require some work to hook into the keystone Prisma client/schema, something I haven't had the time/need to do yet.
Strange though it doesn't work with Google for you. have you got any console errors or minimal reproduction steps/example code you are running?
Hi, @borisno2 sorry for replying late. For the Google provider, I don't get any errors in console. However, in the isAccessAllowed function the context.session is undefined.
Here is the example code that I am running (i removed parts that are irrelevant).
const { withAuth } = createAuth({
listKey: "User",
identityField: "email",
sessionData: `id name email role`,
resolver: async ({ user }: { user: any }) => {
return user;
},
keystonePath: "/",
providers: [
GoogleProvider({
clientId: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
}),
],
sessionSecret,
autoCreate: false,
});
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
// This defines how sessions should work. For more details, check out: https://keystonejs.com/docs/apis/session#session-api
const session = statelessSessions({
maxAge: sessionMaxAge,
secret: sessionSecret!,
});
const auth = withAuth(
// Using the config function helps typescript guide you to the available options.
config({
...
ui: {
isAccessAllowed: (context: KeystoneContext) => {
return context.session?.data?.role === "ADMIN";
},
getAdditionalFiles: [
async () => [
{
mode: "copy",
inputPath: Path.resolve("./cms/customPages/signin.js"),
outputPath: "pages/auth/signin.js",
},
{
mode: "copy",
inputPath: Path.resolve("./cms/customPages/error.js"),
outputPath: "pages/auth/error.js",
},
],
],
},
experimental: {
generateNodeAPI: true,
},
session,
})
)
Also, from the backend/keystone.ts example in this repo, I see pages being used in createAuth but I don't seem to find any documentation on what it's used for. (Maybe I missed it somewhere, forgive me if I did). May I know what it's used for?
Thanks.
Thanks for the update @Jun-Xiang, what versions of @keystone-6/core and @opensaas/keystone-nextjs-auth are you using? have you tried updating to the latest? you will no longer need the experimental configuration once you do. Does it push you over to the google sign in?
The pages config is passed through to next-auth and relates to this https://next-auth.js.org/configuration/options#pages - essentially setting custom pages for next-auth UI.
@borisno2 @keystone-6/core version was at^2.1.0 and @opensaas/keystone-nextjs-auth version is ^24.1.0
I upgraded both packages and removed the experimental but the problem still exists, context.session is still undefined after a successful login.
I tried setting autoCreate to true and the Access Denied is gone but I got redirected to /auth/signin and context.session is still undefined.
Thanks for clarifying the pages config. I have another part of the config still, which i cannot find in nextauth and in the readme in this repo, the resolver which I would like to get a confirmation. After setting autoCreate to true, I notice that it is related to creating a new user if autoCreate is true. Is that all or is there any other things that it does? Thanks.
Hi @Jun-Xiang Can you try upgrading to @keystone-6/core version ^4.0.0 and @opensaas/keystone-nextjs-auth version ^26.0.0? and let me know if this is still happening?
Hi sorry for the late response. It still isn't working unfortunately. This time, it didn't event reach the isAccessAllowed function. It straight shows Access Denied page. I think you can keep this issue closed already as I planned to try others instead of keystone already. Thanks for the help these times.