open-saas icon indicating copy to clipboard operation
open-saas copied to clipboard

Can't sign up after changing the authentication from local to email

Open zacker330 opened this issue 1 year ago • 6 comments

Describe the bug

The email auth is not working.

To Reproduce Steps to reproduce the behavior:

  1. Clone opensaas. We are using opensaas.sh directly.
  2. Change the code as doc said
  3. Change user entity on main.wasp, like
entity User {=psl
  id                        Int             @id @default(autoincrement())
  email                     String?         @unique
  username                  String          @unique
  password                  String?
  createdAt                 DateTime        @default(now())
...
  1. Sign up on the browser
  2. See error on the console:
[Server]  src/auth/providers/email/signup.ts(48,39): error TS2345: Argument of type '{ email: any; password: any; }' is not assignable to parameter of type 'UserCreateInput'.
[Server]    Property 'username' is missing in type '{ email: any; password: any; }' but required in type 'UserCreateInput'.

And If I change the code wasp generated in .wasp/out/server/src/auth/providers/email/signup.ts from

        const user = await createUser({
            ...additionalFields,
            email: userFields.email,
            password: userFields.password,
        });

to

        const user = await createUser({
            ...additionalFields,
            email: userFields.email,
            username: userFields.email,
            password: userFields.password,
        });

The user signed up successfully.

Expected behavior When I change the auth to email auth, the user can sign up correctly.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS 11.7.4
  • Browser Chrome
  • Version 120.0.6099.129

Additional context Add any other context about the problem here.

zacker330 avatar Jan 01 '24 04:01 zacker330

I moved this issue to open-saas repo, as that makes more sense for it!

So the problem here @zacker330 is that username field is not optional on your Prisma model. If you make it optional (with ?), then this will work. However, Wasp by default will not set it to email, instead it will leave the username empty.

Martinsos avatar Jan 02 '24 13:01 Martinsos

I moved this issue to open-saas repo, as that makes more sense for it!

So the problem here @zacker330 is that username field is not optional on your Prisma model. If you make it optional (with ?), then this will work. However, Wasp by default will not set it to email, instead it will leave the username empty.

Thanks for Martinsos.

Let me first explain the complete process.

The Complete Process

Originally, I did set username as a optional field, email and password are required as the following code:

  email                     String         @unique
  username                  String?          @unique
  password                  String

But I got the compile error after I run wasp build or wasp db migrate-dev, like this:

❌ --- [Error] Your wasp project failed to compile: -------------------------------

- Entity 'User' (referenced by app.auth.userEntity) must have field 'email' of type 'String'.
- Entity 'User' (referenced by app.auth.userEntity) must have field 'password' of type 'String'.

Here is the example repo for this issue: https://github.com/zacker330/opensaas-test/commit/7b97dc2873115e80a26feae3bb7296b5a7a69074

When I changed the field to the oppsite, the build is pass. Here the commit for this change https://github.com/zacker330/opensaas-test/commit/66815374a98cf33e5e80be2e3d4f8dccbecc91cd:

  email                     String?         @unique
  username                  String          @unique
  password                  String?

But when I run wasp start, I got another error:

[Server]  > [email protected] build
[Server]  > npx tsc
[Server]
[Server]  src/auth/providers/email/signup.ts(48,39): error TS2345: Argument of type '{ email: any; password: any; }' is not assignable to parameter of type 'UserCreateInput'.
[Server]    Property 'username' is missing in type '{ email: any; password: any; }' but required in type 'UserCreateInput'.
[Server]  [nodemon] app crashed - waiting for file changes before starting...

So, I attempted to fix this new issue by changing .wasp/out/server/src/auth/providers/email/signup.ts file directly as I'd said previously.

I noticed that @Martinsos said leaving the username empty. But it would not be working. username is used by so many places, like UI and UserCreateInput type.

The Root Cause

I think the root cause is that:

  1. The compile error prove that wasp read User entity incorrectly.
  2. The email auth logic that wasp generated is not covering the use case of its.

zacker330 avatar Jan 03 '24 01:01 zacker330

Hm this is very weird @zacker330 , and thanks for all the info!

Regarding root causes that you listed:

  1. I don't think it is likely/possible that Wasp wrongly read the User entity -> something else must be happening there.
  2. It is not that logic that wasp generated is not covering the use case, it is that you can't have a required username in this case, because it doesn't make sense for how currently auth works in Wasp, which is what error says (although in a bit convoluted way).

What I would like to understand is what is causing that first error that you get, with missing pass/email, we will investigate this. This example you provided should help for sure.

As for leaving the username empty -> aha, it is used in many places hm! But what you can do is just use email instead in those places, if you don't have the username, right? Anyway you wanted to set the username to email, so it is the same thing.

@vincanger what are your thoughts on this?

Martinsos avatar Jan 03 '24 13:01 Martinsos

Hey @zacker330,

I believe that there actually is a bug in the Wasp Auth in this case. @infomiho can confirm if this is true or not.

For the time being, you can get your code to compile and run by setting up your User Entity like so:

entity User {=psl
  id                   Int                @id @default(autoincrement())
  email                String?        @unique
  username             String?         @unique
  password             String?
//...

let me know if that works!

and as @Martinsos mentioned, if the client code is looking for a username property but it's undefined, then just change it to use the email property.

or if you want to programmatically set a username for each user on sign up, then you can customize the signup code like so:

// main.wasp

app crudTesting {
  // ...
  auth: {
    userEntity: User,
    methods: {
      email: {}
    },
    onAuthFailedRedirectTo: "/login",
    signup: {
      additionalFields: import { fields } from "@server/auth/signup.js",
    },
  },
}
// signup.ts

import { defineAdditionalSignupFields } from '@wasp/auth/index.js';

export default defineAdditionalSignupFields({
  username: (data) => {
    return data.email as string; // if using typescript you need to cast the type as a string
  },
});

the code above will set the email as the username as well.

also, you should never have to touch the outputted code in the .wasp directory while developing! this folder is where Wasp compiles your code to for you :)

vincanger avatar Jan 03 '24 16:01 vincanger

Same problem here

MiqueiasGFernandes avatar Feb 05 '24 17:02 MiqueiasGFernandes

Same problem here

@MiqueiasGFernandes try making the email, username, and password fields of your user entity optional (?), like this:

entity User {=psl
  id                        Int             @id @default(autoincrement())
  email                     String?         @unique
  username                  String?         @unique
  password                  String?
  //...

Let me know if that works! :)

vincanger avatar Feb 05 '24 17:02 vincanger

hey @MiqueiasGFernandes & @zacker330

the newest version of the template runs on Wasp v0.12 and has updated Authentication methods! So migrating to new auth methods shouldn't be an issue anymore 👍

give it a try here https://docs.opensaas.sh/start/getting-started/#cloning-the-opensaas-template

I'm closing this issue for now, but feel free to respond here if you have any questions and I'll reopen it :)

vincanger avatar Feb 28 '24 13:02 vincanger