Permalink
Browse files

back-end complete auth setup up but commented out

  • Loading branch information...
michaelion committed Nov 8, 2019
1 parent fac1993 commit cc3da18800e7e7adbb4cff61e6e9b059ed58a2b0
Showing 1,528 changed files with 24,654 additions and 10 deletions.
@@ -15,6 +15,7 @@ gem 'puma', '~> 4.1'
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'jwt'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
@@ -23,7 +24,7 @@ gem 'bcrypt', '~> 3.1.7'
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'
gem 'rack-cors'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
@@ -32,6 +33,8 @@ end

group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'better_errors'
gem 'binding_of_caller'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
@@ -57,18 +57,27 @@ GEM
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
bcrypt (3.1.13)
better_errors (2.5.1)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootsnap (1.4.5)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.5)
debug_inspector (0.0.3)
erubi (1.9.0)
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jwt (2.2.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@@ -93,6 +102,7 @@ GEM
puma (4.2.1)
nio4r (~> 2.0)
rack (2.0.7)
rack-cors (1.0.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.0.1)
@@ -151,11 +161,15 @@ PLATFORMS

DEPENDENCIES
bcrypt (~> 3.1.7)
better_errors
binding_of_caller
bootsnap (>= 1.4.2)
byebug
jwt
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 4.1)
rack-cors
rails (~> 6.0.1)
spring
spring-watcher-listen (~> 2.0.0)
@@ -1,2 +1,30 @@
class ApplicationController < ActionController::API


# SECRET_KEY = Rails.application.secrets.secret_key_base.to_s

# def encode(payload, exp = 24.hours.from_now)
# payload[:exp] = exp.to_i
# JWT.encode(payload, SECRET_KEY)
# end

# def decode(token)
# decoded = JWT.decode(token, SECRET_KEY)[0]
# HashWithIndifferentAccess.new decoded
# end



# def authorize_request
# header = request.headers['Authorization']
# header = header.split(' ').last if header
# begin
# @decoded = decode(header)
# @current_user = User.find(@decoded[:user_id])
# rescue ActiveRecord::RecordNotFound => e
# render json: { errors: e.message }, status: :unauthorized
# rescue JWT::DecodeError => e
# render json: { errors: e.message }, status: :unauthorized
# end
# end
end
@@ -0,0 +1,25 @@
class AuthenticationController < ApplicationController
# before_action :authorize_request, except: :login

# def login
# @user = User.find_by_username(params[:username])
# if @user.authenticate(params[:password])
# token = encode(user_id: @user.id, username: @user.username)
# render json: { user: @user, token: token}, status: :ok
# else
# render json: { error: 'unauthorized'}, status: :unauthorized
# end
# end

# def verify
# render json: @current_user, status: :ok
# end


# private

# def login_params
# params.permit(:username, :password)
# end

end
@@ -0,0 +1,51 @@
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy]

# GET /posts
def index
@posts = Post.all

render json: @posts
end

# GET /posts/1
def show
render json: @post
end

# POST /posts
def create
@post = Post.new(post_params)

if @post.save
render json: @post, status: :created, location: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /posts/1
def update
if @post.update(post_params)
render json: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end

# DELETE /posts/1
def destroy
@post.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def post_params
params.require(:post).permit(:text, :user_id)
end
end
@@ -0,0 +1,53 @@
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
# before_action :authorize_request, except: :create

# GET /users
def index
@users = User.all

render json: @users

end

# GET /users/1
def show
render json: @user
end

# POST /users
def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /users/1
def destroy
@user.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :usr_name, :password)
end
end
@@ -0,0 +1,3 @@
class Post < ApplicationRecord
belongs_to :user
end
@@ -0,0 +1,12 @@
class User < ApplicationRecord
has_many :posts
has_secure_password

validates :usr_name, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, length: { minimum: 6}
end


# :first_name, :last_name, :email, :usr_name, :password
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `yarn build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Oops, something went wrong.

0 comments on commit cc3da18

Please sign in to comment.