-
Notifications
You must be signed in to change notification settings - Fork 1
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
Authenticate/Inviting #1
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
43f01d3
feature: Adding adding authentication by jwt
mikeoleynik e11e918
feature: Adding ability to send an invitate
mikeoleynik 8b90bd0
feature: Adding a new user
mikeoleynik e01c889
Adding: init setup
mikeoleynik 6a03799
chore: Adding devise
mikeoleynik 7951cc4
chore: Made authentication for all controllers (transfer to Applicati…
mikeoleynik bd382a8
fix: Adding UsersController#create for create user (and send invite)
mikeoleynik 3541741
chore: Adding constraint for user fullname
mikeoleynik 945b275
chore: Adding gem faker for test
mikeoleynik 209a94b
chore: Removing default value for user fullname
mikeoleynik 2429958
chore: Adding error output when creating a default user
mikeoleynik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,13 @@ | ||
# README | ||
## Getting Started | ||
|
||
This README would normally document whatever steps are necessary to get the | ||
application up and running. | ||
This section provides quick start guide. | ||
|
||
Things you may want to cover: | ||
### Prerequisites | ||
|
||
* Ruby version | ||
- [Ruby](https://www.ruby-lang.org/en/): 2.6.3 | ||
- [Ruby on Rails](https://rubyonrails.org/): 2.6.3 | ||
- [PostgreSQL](https://www.postgresql.org/) 9.4 or higher. | ||
|
||
* System dependencies | ||
### Standard Installation | ||
|
||
* Configuration | ||
|
||
* Database creation | ||
|
||
* Database initialization | ||
|
||
* How to run the test suite | ||
|
||
* Services (job queues, cache servers, search engines, etc.) | ||
|
||
* Deployment instructions | ||
|
||
* ... | ||
1. `bin/setup` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::API | ||
rescue_from BasicAuthenticate::NotAuthenticated, with: :not_authenticated | ||
|
||
private | ||
|
||
def authorize! | ||
AuthenticateByToken.new(request.headers['Authorization']).call | ||
end | ||
|
||
def not_authenticated | ||
render json: { error: 'Not Authenticated' }, status: :unauthorized | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuthenticationController < ApplicationController | ||
# POST /login | ||
def create | ||
token = GetToken.new(email: params[:email], password: params[:password]).call | ||
render json: { auth_token: token }, status: :ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class HomeController < ApplicationController | ||
before_action :authorize! | ||
# GET / | ||
def homepage | ||
render json: { success: 'ok' }, status: :ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class InvitesController < ApplicationController | ||
before_action :authorize! | ||
# GET /send_invite | ||
def send_invite | ||
prefix = SecureRandom.hex(3) | ||
InviteMailer.with(email: "#{prefix}[email protected]", password: 'qwerty').send_invite.deliver_later | ||
render json: { success: 'invite sent' }, status: :ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class UsersController < ApplicationController | ||
# POST /users | ||
def create | ||
user = User.new(user_params) | ||
if user.save | ||
render json: { success: ['User created'] }, status: :ok | ||
else | ||
render json: { error: user.errors.full_messages }, status: :internal_server_error | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.permit(:email, :password) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class InviteMailer < ApplicationMailer | ||
def send_invite | ||
@email = params[:email] | ||
@password = params[:password] | ||
|
||
mail(to: @email, subject: 'Welcome') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class User < ApplicationRecord | ||
has_secure_password | ||
|
||
validates :email, :password, presence: true | ||
validates :email, uniqueness: true | ||
validates :password, length: { in: 8..20 } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuthenticateByToken | ||
include BasicAuthenticate | ||
|
||
attr_reader :token | ||
|
||
def initialize(token) | ||
@token = token | ||
end | ||
|
||
def call | ||
user_id = decode_payload(token) | ||
User.find(user_id['sub']) | ||
rescue ActiveRecord::RecordNotFound, JWT::DecodeError | ||
raise NotAuthenticated | ||
end | ||
|
||
private | ||
|
||
def decode_payload(payload) | ||
JWT.decode(payload, secret).first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class GetToken | ||
include BasicAuthenticate | ||
|
||
attr_reader :email, :password | ||
|
||
def initialize(email:, password:) | ||
@email = email | ||
@password = password | ||
end | ||
|
||
def call | ||
user = User.find_by(email: email) | ||
if user&.authenticate(password) | ||
payload = { 'sub' => user.id } | ||
encode_payload(payload) | ||
else | ||
raise NotAuthenticated | ||
end | ||
end | ||
|
||
private | ||
|
||
def encode_payload(payload) | ||
JWT.encode(payload, secret) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
%p Ваш логин: #{@email} | ||
%p Ваш пароль: #{@password} | ||
|
||
Нажмите, чтобы продолжить: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
root 'home#homepage' | ||
post '/login', to: 'authentication#create' | ||
get '/send_invite', to: 'invites#send_invite' | ||
post '/users', to: 'users#create' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateUsers < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :users do |t| | ||
t.string :email, null: false, unique: true | ||
t.string :password_digest, null: false | ||
t.boolean :mentor, default: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 2019_09_06_052935) do | ||
|
||
# These are extensions that must be enabled in order to support this database | ||
enable_extension "plpgsql" | ||
|
||
create_table "users", force: :cascade do |t| | ||
t.string "email", null: false | ||
t.string "password_digest", null: false | ||
t.boolean "mentor", default: false | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
# This file should contain all the record creation needed to seed the database with its default values. | ||
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). | ||
# | ||
# Examples: | ||
# | ||
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) | ||
# Character.create(name: 'Luke', movie: movies.first) | ||
User.create(email: '[email protected]', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create! |
||
password: 'qwerty12', | ||
password_confirmation: 'qwerty12') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module BasicAuthenticate | ||
class NotAuthenticated < StandardError; end | ||
|
||
private | ||
|
||
def secret | ||
@secret = Rails.application.secrets.secret_key_base | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
email { '[email protected]' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gem faker |
||
password { 'qwerty12' } | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
зачем свою писать, если есть sorcery или devise?