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

Idea for an example (JWT Auth Server) #2838

Open
gautvm opened this issue May 6, 2021 · 5 comments
Open

Idea for an example (JWT Auth Server) #2838

gautvm opened this issue May 6, 2021 · 5 comments
Labels
kind/feature A request for a new feature. topic: new example

Comments

@gautvm
Copy link

gautvm commented May 6, 2021

I figured creating an example of making a JWT auth server using Prisma might be useful, with access tokens, user storing, sessions, password hashing, isAuth middleware, etc.

The routes would look like this

POST

  • /register: Register a user which returns an access token to log in.
    • Body:
      • email: String (required): The email of the user
      • username: String (required): The username of the user registering
      • password: String (required): The password of the user registering
  • /login: Log in a user which will return an access token to access protected routes.
    • Body:
      • username: String (required): The username of the user logging in
      • password: String (required): The password of the user logging in
  • /logout (protected): Logout a user and delete the session key.

DELETE

  • /account/delete (protected): Delete your account

PUT

  • /account/update: Update your account
    • Body:
      • email: String (optional): used if the email is being updated
      • username: String (optional): used if the username is being updated
      • password: String (optional): used if the password is being updated

I have a GitHub repo with some of it implemented, though I am going to implement sessions as well.

If this idea gets approved, I will write the example in a way that follows the style of the other examples. I could update this proposed idea in any way that the Prisma team will like it to be.

The data model would look like this

model User {
  id Int @id @default(autoincrement())
  username String @unique
  email String @unique
  password String
  sessionKey String
}
@ruheni
Copy link
Contributor

ruheni commented May 7, 2021

This is a great idea!

We can go ahead with this.

Before you do, I have a few recommendations. We like to maintain consistency and make them as simple as possible for beginners and experienced developers.

  • Borrow inspiration from the other rest-* examples for the structure of the example.
  • You can also borrow inspiration from our graphql-auth examples for the extra routes you could add to the example

A general rule of thumb is to test the example to ensure they are working as expected locally before a review. We're working on automating the testing for the *-auth examples.

Once you're done creating the example, add the test in the relevant test .github/test folder. You add a README in the .github./readmes folder and follow our Contributing Guide on generating a README.

Lastly, include the example and the link in the repo's main README file under the relevant section.

If you hit a snag, don't hesitate to let us know. 🙂

@gautvm
Copy link
Author

gautvm commented May 7, 2021

Thanks! Will do.

@IBRAHIMDANS
Copy link

Good idea, but I think it's more optimized to have a new model PasswordModel to allow the user to have several login methods. Also i check if new password are used by user
for example:
Users (id, email, passwordId, passwords []) ===> Password(id, userId, isActive, createdAt, updatedAt)

@gautvm
Copy link
Author

gautvm commented Mar 7, 2023

Good idea, but I think it's more optimized to have a new model PasswordModel to allow the user to have several login methods. Also i check if new password are used by user for example: Users (id, email, passwordId, passwords []) ===> Password(id, userId, isActive, createdAt, updatedAt)

I think it might be better to have an enum like OAuthConnectionType rather than a Password model. Feel free to implement this in the PR, if you'd like (I'll be finishing it up soon).

@XprabhatX
Copy link

@gautvm is it implemented yet? Because I saw some tests in pr #2869 are failing (But it's an old pr and this issue is open).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. topic: new example
Projects
None yet
Development

No branches or pull requests

5 participants