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

[✨]Could add an example of Auth.js that uses username+password? #5452

Closed
zwl1619 opened this issue Nov 17, 2023 · 11 comments · Fixed by #5462
Closed

[✨]Could add an example of Auth.js that uses username+password? #5452

zwl1619 opened this issue Nov 17, 2023 · 11 comments · Fixed by #5462
Labels
STATUS-1: needs triage New issue which needs to be triaged TYPE: enhancement New feature or request

Comments

@zwl1619
Copy link

zwl1619 commented Nov 17, 2023

Could add an example of Auth.js that uses username+password?
Seems many people have trouble with it.
There is an example that uses GitHub auth in the docs here: https://qwik.builder.io/docs/integrations/authjs/
But it's different from username+password.

@zwl1619 zwl1619 added STATUS-1: needs triage New issue which needs to be triaged TYPE: enhancement New feature or request labels Nov 17, 2023
@VarPDev
Copy link
Contributor

VarPDev commented Nov 18, 2023

I would like to work on this

@gioboa
Copy link
Member

gioboa commented Nov 18, 2023

Thanks @VarPDev

@zwl1619
Copy link
Author

zwl1619 commented Nov 18, 2023

@VarPDev That's great! Could use jwt instead of session?

@VarPDev
Copy link
Contributor

VarPDev commented Nov 19, 2023

@zwl1619 I saw there is a property you can change inside serverAuth$ named "session"

Example

export const { onRequest, useAuthSession, useAuthSignin, useAuthSignout } =
  serverAuth$(({ env }) => ({
    ...
   // with this I think you can use jwt instead session
    session: { strategy: "jwt" },
    providers: [
      ...
    ] as Provider[],
   ...
  }));

@zwl1619
Copy link
Author

zwl1619 commented Nov 19, 2023

@VarPDev Thanks! Could you please use jwt in the username+ password example? Session has already been used in the github auth example, showing different ways is always better.

@VarPDev
Copy link
Contributor

VarPDev commented Nov 19, 2023

Auth.js says jwt is the default choice.

Auth.js libraries can create sessions using JSON Web Tokens (JWT). This is the default session strategy for Auth.js libraries.

Need to figure out if it is overridden by qwik or remains default jwt as in auth.js.
This would be best to ask someone who has worked on it: @gioboa do you know anyone who knows if jwt is used by default? At first look it seems to me that the default one is used.

@gioboa
Copy link
Member

gioboa commented Nov 19, 2023

I don't have clue for that. if JWT is the default option, go for it.

@VarPDev
Copy link
Contributor

VarPDev commented Nov 19, 2023

@zwl1619

I did a PR to update the documentation.
For now here the example to use Credentials:

file: plugin@auth.ts

import { serverAuth$ } from "@builder.io/qwik-auth";
import type { Provider } from "@auth/core/providers";
import Credentials from "@auth/core/providers/credentials";

export const { onRequest, useAuthSession, useAuthSignin, useAuthSignout } =
  serverAuth$(({ env }) => ({
    secret: env.get("AUTH_SECRET"),
    trustHost: true,
    providers: [
      Credentials({
        async authorize(credentials, req) {
          // Add logic here to look up the user from the credentials supplied
          const user = {
            id: "1",
            name: "J Smith",
            email: "jsmith@example.com",
          };

          return user;
        },
      }),
    ] as Provider[],
  }));

file: sign.ts

import { component$ } from "@builder.io/qwik";
import { Form } from "@builder.io/qwik-city";
import { useAuthSignin } from "./plugin@auth";

export default component$(() => {
  const signIn = useAuthSignin();

  return (
    <>
      <Form action={signIn}>
        <input type="hidden" name="providerId" value="credentials" />
        <input
          type="hidden"
          name="options.callbackUrl"
          value="http://qwik-auth-example.com/dashboard"
        />
        <input name="options.email" type="email" />
        <input name="options.password" type="password" />
        <button>Sign In</button>
      </Form>
    </>
  );
});

@gioboa gioboa linked a pull request Nov 19, 2023 that will close this issue
7 tasks
@zwl1619
Copy link
Author

zwl1619 commented Nov 20, 2023

Thanks very much!

@zwl1619 zwl1619 closed this as completed Nov 20, 2023
@gioboa
Copy link
Member

gioboa commented Nov 20, 2023

@zwl1619 did you try it?

@zwl1619
Copy link
Author

zwl1619 commented Nov 20, 2023

@gioboa Not yet! I'll try it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
STATUS-1: needs triage New issue which needs to be triaged TYPE: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants