Skip to content

Commit c53d18d

Browse files
committed
Add Sign up through API
1 parent 8182ec4 commit c53d18d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

app/controllers/api/authentication_controller.rb

+22
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,26 @@ def create
1414
render json: { error: 'Invalid credentials' }, status: :unauthorized
1515
end
1616
end
17+
18+
def signup
19+
user = User.new(user_params)
20+
21+
if user.save
22+
token = user.generate_jwt
23+
render json: {
24+
message: 'Signup successful',
25+
token: token
26+
}, status: :created
27+
else
28+
render json: {
29+
errors: user.errors.full_messages
30+
}, status: :unprocessable_entity
31+
end
32+
end
33+
34+
private
35+
36+
def user_params
37+
params.require(:user).permit(:email, :password, :password_confirmation)
38+
end
1739
end

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
root "pages#index"
1111

1212
namespace :api do
13+
post 'signup', to: 'authentication#signup'
1314
post 'login', to: 'authentication#create'
1415
end
1516

0 commit comments

Comments
 (0)