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

Sign In with Credentials Provider always return OK #66

Open
Lenghak opened this issue Feb 21, 2024 · 3 comments
Open

Sign In with Credentials Provider always return OK #66

Lenghak opened this issue Feb 21, 2024 · 3 comments

Comments

@Lenghak
Copy link

Lenghak commented Feb 21, 2024

Reasoning

I have been trying to implement error handling with auth-astro after managed to have sign in function works. However, there is an issue that making the return object from signIn with { redirect : false } always get 200 as the return status.

  • Down here is my configuration file
import postSignIn from "@/modules/auth/services/sign-in-api";

import Credentials from "@auth/core/providers/credentials";
import { defineConfig } from "auth-astro";

export default defineConfig({
  providers: [
    Credentials({
      id: "credentials",
      authorize: async (credentials, _) => {
        if (!credentials.email || !credentials.password) return null;

        try {
          const res = await postSignIn({
            email: credentials.email as string,
            password: credentials.password as string,
          });

          return {
            ...res.data.data,
            ...res.data.meta,
          };
        } catch (err) {
          return null;
        }
      },
    }),
  ],
  callbacks: {
    jwt: ({ token, user }) => {
      return {
        ...token,
        ...user,
      };
    },
    session: ({ session, token }) => {
      session.user = {
        id: token.id,
        type: token.type,
        emailVerified: new Date(token.attributes.confirmed_at),
        ...token.attributes,
      };

      session.tokens = {
        iat: token.iat,
        exp: token.exp,
        jti: token.jti,
        sub: token.sub,
        at: token.accessToken,
        rt: token.refreshToken,
      };
      return {
        ...session,
      };
    },
  },
  pages: {
    error: "/auth/error",
    newUser: "/onboarding/welcome",
    signIn: "/auth/sign-in",
    signOut: "/",
    verifyRequest: "/verify",
  },
  session: {
    strategy: "jwt",
  },
  trustHost: true,
});
  • Down here is where I used the sign in
...
  return (
    <Form {...form}>
      <form
        method="POST"
        onSubmit={form.handleSubmit(async (values) => {
          const res = await signIn("credentials", {
            redirect: false,
            callbackUrl: "/auth/sign-in",
            ...values,
          });

          console.log(res, await res?.json());
        })}
        className="w-full space-y-2"
      >
...
  • Down here is the console
body: (...),
bodyUsed: true,
headers: Headers {},
ok: true,
redirected: false,
status: 200,
statusText: "OK",
type: "basic",
url: "http://localhost:4321/api/auth/callback/credentials?"
@xino1010
Copy link

xino1010 commented Jul 6, 2024

Any news?

@Lenghak
Copy link
Author

Lenghak commented Jul 9, 2024

I am afraid not. I am still checking the response manually on the error callback url. I am moving to lucia btw.

@xino1010
Copy link

I've implemented my own authorization system...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants