Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Auth #33

Merged
merged 4 commits into from
Nov 5, 2021
Merged

Auth #33

merged 4 commits into from
Nov 5, 2021

Conversation

aroddick
Copy link
Collaborator

@aroddick aroddick commented Nov 4, 2021

Add /register and /login endpoints

/register

  • Checks if username is taken and returns 409 if it does
  • Checks if all fields are provided and returns 422 if not
  • If username is not taken, returns 201 and the jwt and expire time
  • Stores password hash in db

/login

  • Checks if username is valid
  • Checks if password hash matches for the given user
  • Like /register, returns jwt and expire time

Comment on lines +11 to +13
@token = encode_token({ user_id: @user.id })
time = Time.now + 2.hours.to_i
render json: { user: UserSerializer.new(@user), jwt: @token, exp: time }, status: :created
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify the intent here, but is the reason why you're returning a JWT token here as well because when a user registers, it should also automatically log them in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is how the behavior is now just so you don't need to take two steps. It can easily be changed later if we don't want that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it the way you did it. Just making sure I'm understanding it correctly

Comment on lines +17 to +19
def user_login_params
params.require(:user).permit(:username, :password)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require the frontend to send a JSON object in the following format:

{
    "user": {
        "username": "test3",
        "password": "testpw3"
    }
}

Nothing wrong with this functionality-wise, but just making sure that this is how you want it to be?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that is how I wrote it and tested it. We can just accept the username and password instead of a user json if you want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way is fine. We just have to make sure the frontend people know the POST body should look like this for login

@aroddick aroddick merged commit 6ad44cd into main Nov 5, 2021
@aroddick aroddick deleted the auth branch November 5, 2021 03:14
This was linked to issues Nov 9, 2021
@aroddick aroddick self-assigned this Nov 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

POST register POST login
2 participants