forked from avvazana/Spoofify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
5,249 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "esversion":6 } |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
bundle.js | ||
bundle.js.map |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
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,3 @@ | ||
// Place all the styles related to the Users controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
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,23 @@ | ||
class Api::SessionsController < ApplicationController | ||
def create | ||
@user = User.find_by_credentials( | ||
params[:user][:username], | ||
params[:user][:password] | ||
) | ||
if @user | ||
login!(@user) | ||
render 'api/users/show' | ||
else | ||
render json: ["Invalid credentials"], status: 401 | ||
end | ||
end | ||
|
||
def destroy | ||
if current_user | ||
logout! | ||
render json: {} | ||
else | ||
render json: ["No user logged in"], status: 404 | ||
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,23 @@ | ||
class Api::UsersController < ApplicationController | ||
|
||
def create | ||
@user = User.new(user_params) | ||
|
||
if @user.save | ||
login(@user) | ||
render 'api/users/show' | ||
else | ||
render json: @user.errors.full_messages, status: 422 | ||
end | ||
end | ||
|
||
def show | ||
@user = User.find(params[:id]) | ||
render :show | ||
end | ||
|
||
private | ||
def user_params | ||
params.require(:user).permit(:username, :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 |
---|---|---|
@@ -1,2 +1,27 @@ | ||
class ApplicationController < ActionController::Base | ||
helper_method :logged_in?, :current_user | ||
|
||
def current_user | ||
@current_user ||= User.find_by_session_token(session[:session_token]) | ||
end | ||
|
||
def logged_in? | ||
!!current_user | ||
end | ||
|
||
def ensure_logged_in | ||
unless current_user | ||
render json: {base: ['invalid credentials']}, status: 401 | ||
end | ||
end | ||
|
||
def login!(user) | ||
session[:session_token] = user.reset_session_token! | ||
@current_user = user | ||
end | ||
|
||
def logout! | ||
current_user.reset_session_token! | ||
session[:session_token] = nil | ||
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 @@ | ||
class StaticPagesController < ApplicationController | ||
def root | ||
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,2 @@ | ||
module UsersHelper | ||
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,32 @@ | ||
class User < ApplicationRecord | ||
validates :username, :session_token, :email, presence: true, uniqueness: true | ||
validates :password_digest, presence: {message: "Password can't be blank"} | ||
validates :password, length: {minimum: 6, allow_nil: true} | ||
|
||
after_initialize :ensure_session_token | ||
attr_reader :password | ||
|
||
def password=(password) | ||
@password = password | ||
self.password_digest = BCrypt::Password.create(password) | ||
end | ||
|
||
def self.find_by_credentials(username, password) | ||
user = User.find_by(username: username) | ||
user && user.is_password?(password) ? user : nil | ||
end | ||
|
||
def is_password?(password) | ||
BCrypt::Password.new(self.password_digest).is_password?(password) | ||
end | ||
|
||
def ensure_session_token | ||
self.session_token ||= SecureRandom.urlsafe_base64(16) | ||
end | ||
|
||
def reset_session_token! | ||
self.session_token = SecureRandom.urlsafe_base64(16) | ||
self.save! | ||
self.session_token | ||
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 @@ | ||
json.extract! user, :id, :username |
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,2 @@ | ||
debugger | ||
json.partial! 'api/users/user', user: @user |
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 @@ | ||
<div id='root'></div> |
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,8 @@ | ||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
root to: "static_pages#root" | ||
|
||
namespace :api, defaults: { format: :json } do | ||
resources :users, only: [:show, :create] | ||
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,16 @@ | ||
class CreateUsers < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :users do |t| | ||
t.string 'username', null: false | ||
t.string 'email', null: false | ||
t.string 'password_digest', null: false | ||
t.string 'session_token', null: false | ||
t.string 'profile_pic_url' | ||
t.datetime 'created_at', null: false | ||
t.datetime 'updated_at', null: false | ||
t.index ['email'], name: 'index_users_email', unique: true | ||
t.index ['session_token'], name: 'index_users_session_token', unique: true | ||
t.index ['username'], name: 'index_users_username', unique: true | ||
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,31 @@ | ||
# 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: 2018_11_13_164249) 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 "username", null: false | ||
t.string "email", null: false | ||
t.string "password_digest", null: false | ||
t.string "session_token", null: false | ||
t.string "profile_pic_url" | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
t.index ["email"], name: "index_users_email", unique: true | ||
t.index ["session_token"], name: "index_users_session_token", unique: true | ||
t.index ["username"], name: "index_users_username", unique: true | ||
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,43 @@ | ||
import * as SessionApiUtil from '../util/session_api_util'; | ||
|
||
export const RECEIVE_CURRENT_USER = "RECEIVE_CURRENT_USER"; | ||
export const RECEIVE_USER = 'RECEIVE_USER'; | ||
export const LOGOUT_CURRENT_USER = "LOGOUT_CURRENT_USER"; | ||
|
||
export const receiveCurrentUser = user => ({ | ||
type: RECEIVE_CURRENT_USER, | ||
user | ||
}); | ||
|
||
export const receiveUser = user => ({ | ||
type: RECEIVE_USER, | ||
user | ||
}); | ||
|
||
const logoutCurrentUser = () => ({ | ||
type: LOGOUT_CURRENT_USER | ||
}); | ||
|
||
export const signup = user => dispatch => { | ||
return SessionApiUtil.signup(user) | ||
.then(user => dispatch(receiveCurrentUser(user)), | ||
err => dispatch(receiveSignupErrors(err.responseJSON))); | ||
}; | ||
|
||
export const login = user => dispatch => { | ||
return SessionApiUtil.login(user) | ||
.then(user => dispatch(receiveCurrentUser(user)), | ||
err => dispatch(receiveSigninErrors(err.responseJSON))); | ||
}; | ||
|
||
export const logout = () => dispatch => { | ||
return SessionApiUtil.logout() | ||
.then( () => dispatch(logoutCurrentUser()), | ||
err => console.log(err.responseJSON)) | ||
}; | ||
|
||
export const fetchUser = id => dispatch => { | ||
return SessionApiUtil.logout() | ||
.then( user => dispatch(receiveUser(user)), | ||
err => console.log(err.responseJSON)) | ||
}; |
Oops, something went wrong.