Skip to content
This repository was archived by the owner on Aug 8, 2019. It is now read-only.

Commit 6d748b2

Browse files
author
Lian Nivin
authored
Merge pull request #12 from codeableorg/feature-register-and-login-user
Feature register and login user
2 parents 83f81a2 + 2f98b7b commit 6d748b2

17 files changed

Lines changed: 487 additions & 31 deletions

File tree

api/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gem 'puma', '~> 3.11'
1414
# Use Redis adapter to run Action Cable in production
1515
# gem 'redis', '~> 4.0'
1616
# Use ActiveModel has_secure_password
17-
# gem 'bcrypt', '~> 3.1.7'
17+
gem 'bcrypt', '~> 3.1.7'
1818

1919
# Use ActiveStorage variant
2020
# gem 'mini_magick', '~> 4.8'

api/Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ GEM
4848
minitest (~> 5.1)
4949
tzinfo (~> 1.1)
5050
arel (9.0.0)
51+
bcrypt (3.1.13)
5152
bootsnap (1.4.4)
5253
msgpack (~> 1.0)
5354
builder (3.2.3)
@@ -160,6 +161,7 @@ PLATFORMS
160161

161162
DEPENDENCIES
162163
active_model_serializers
164+
bcrypt (~> 3.1.7)
163165
bootsnap (>= 1.1.0)
164166
byebug
165167
listen (>= 3.0.5, < 3.2)
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
class ApplicationController < ActionController::API
2-
end
2+
include ActionController::Cookies
3+
4+
before_action :require_login
5+
6+
def require_login
7+
authenticate_token || render_unauthorized('Access denied')
8+
end
9+
10+
def current_user
11+
@current_user ||= authenticate_token
12+
end
13+
14+
private
15+
16+
def render_unauthorized(message)
17+
errors = { errors: { message: message } }
18+
render json: errors, status: :unauthorized
19+
end
20+
21+
def authenticate_token
22+
user = User.find_by_token(cookies.signed[:auth_token])
23+
regenerate_and_signed_token(user) if user
24+
end
25+
26+
def regenerate_and_signed_token(user)
27+
user.regenerate_token
28+
cookies.signed[:auth_token] = { value: user.token, httponly: true }
29+
user
30+
end
31+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class SessionsController < ApplicationController
2+
skip_before_action :require_login, only: [:register, :login]
3+
4+
def login
5+
user = User.valid_login?(params[:email], params[:password])
6+
if user
7+
regenerate_and_signed_token(user)
8+
render json: user
9+
else
10+
render json: { errors: 'Incorrect email or password' },
11+
status: :bad_request
12+
end
13+
end
14+
15+
def register
16+
user = User.new(user_params)
17+
if user.save
18+
render json: user
19+
else
20+
render json: { errors: user.errors}
21+
end
22+
23+
end
24+
25+
def destroy
26+
current_user.invalidate_token
27+
cookies.delete :auth_token
28+
head :ok
29+
end
30+
31+
private
32+
def user_params
33+
params.permit(:name, :email, :password, :role)
34+
end
35+
end

api/app/models/user.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class User < ApplicationRecord
2+
has_secure_password
3+
has_secure_token
4+
5+
validates :email, :name, :role, presence: true
6+
validates :email, uniqueness: true
7+
8+
9+
10+
def invalidate_token
11+
update(token: nil)
12+
end
13+
14+
def self.valid_login?(email, password)
15+
user = find_by(email: email)
16+
user if user&.authenticate(password)
17+
end
18+
end

api/config/initializers/cors.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# Be sure to restart your server when you modify this file.
1+
Rails.application.config.middleware.insert_before 0, Rack::Cors do
2+
allow do
3+
origins 'localhost:3000'
24

3-
# Avoid CORS issues when API is called from the frontend app.
4-
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
5-
6-
# Read more: https://github.com/cyu/rack-cors
7-
8-
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
9-
# allow do
10-
# origins 'example.com'
11-
#
12-
# resource '*',
13-
# headers: :any,
14-
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
15-
# end
16-
# end
5+
resource '*',
6+
headers: :any,
7+
methods: [:get, :post, :put, :patch, :delete, :options, :head],
8+
credentials: true
9+
end
10+
end

api/config/routes.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
Rails.application.routes.draw do
2-
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
2+
scope '/api' do
3+
# sessions routes
4+
post '/login', to: 'sessions#login'
5+
post '/register', to: 'sessions#register'
6+
7+
delete '/logout', to: 'sessions#destroy'
8+
9+
end
310
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateUsers < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :token
6+
t.string :email
7+
t.string :password_digest
8+
t.string :role
9+
10+
t.timestamps
11+
end
12+
add_index :users, :token
13+
end
14+
end

api/db/schema.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_07_09_175149) do
13+
ActiveRecord::Schema.define(version: 2019_07_11_201636) do
14+
15+
# These are extensions that must be enabled in order to support this database
16+
enable_extension "plpgsql"
1417

1518
create_table "active_storage_attachments", force: :cascade do |t|
1619
t.string "name", null: false
1720
t.string "record_type", null: false
18-
t.integer "record_id", null: false
19-
t.integer "blob_id", null: false
21+
t.bigint "record_id", null: false
22+
t.bigint "blob_id", null: false
2023
t.datetime "created_at", null: false
2124
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
2225
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
@@ -41,4 +44,16 @@
4144
t.datetime "updated_at", null: false
4245
end
4346

47+
create_table "users", force: :cascade do |t|
48+
t.string "name"
49+
t.string "token"
50+
t.string "email"
51+
t.string "password_digest"
52+
t.string "role"
53+
t.datetime "created_at", null: false
54+
t.datetime "updated_at", null: false
55+
t.index ["token"], name: "index_users_on_token"
56+
end
57+
58+
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
4459
end

api/db/seeds.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# This file should contain all the record creation needed to seed the database with its default values.
2-
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3-
#
4-
# Examples:
5-
#
6-
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7-
# Character.create(name: 'Luke', movie: movies.first)
1+
User.destroy_all
2+
User.create(name: 'Lian Nivin', email: 'liam@kampu.pe', role: "regular", password: '123456')
3+
User.create(name: 'Cristian Berly', email: 'berli@kampu.pe', role: "owner", password: '123456')

0 commit comments

Comments
 (0)