Skip to content
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

AbrarNa'im #7

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6fd626c
sedang proses validasi
abrarnaim015 Oct 26, 2020
a3311f7
AbrarNaim
abrarnaim015 Oct 26, 2020
260f818
Abrar Naim D1 Done
abrarnaim015 Oct 26, 2020
8616245
Abrar Naim D1 Done
abrarnaim015 Oct 26, 2020
5605169
Abrar Naim D2 proses logIn & register
abrarnaim015 Oct 27, 2020
fce162a
Abrar Naim D2 proses logIn midelware
abrarnaim015 Oct 27, 2020
7184f60
Abrar Naim D2 proses midelware & perbaikan TODO
abrarnaim015 Oct 27, 2020
b637461
Abrar Naim D2 proses Authentication
abrarnaim015 Oct 27, 2020
b780cce
Abrar Naim D2 Authentication & Authorization DONE
abrarnaim015 Oct 27, 2020
423b8ee
Abrar Naim D2 proses errorhandler
abrarnaim015 Oct 27, 2020
9ec93d6
Abrar Naim D2 ErrorHandler DONE
abrarnaim015 Oct 28, 2020
b10b89f
Abrar Naim D2 ErrorHandler DONE
abrarnaim015 Oct 28, 2020
29ec564
Abrar Naim D3 Revisi Helper, bcrypt, jwt ==> DONE
abrarnaim015 Oct 28, 2020
8391561
Abrar Naim D3 revisi ErrorHandler
abrarnaim015 Oct 28, 2020
b41e2f7
Add 3rd API binaryjazz.us/wp-json/genrenator/v1
abrarnaim015 Oct 29, 2020
4a11d37
Add 3rd API add HTML, CSS, jQuery, Form user login
abrarnaim015 Oct 30, 2020
5ba99e9
Add 3rd API add HTML, CSS, jQuery, Form user login
abrarnaim015 Oct 30, 2020
27c3fe6
memperbaiki eror di server
abrarnaim015 Oct 30, 2020
222f57c
add login, home page, and logout with token
abrarnaim015 Oct 30, 2020
bc62d13
add singup proces
abrarnaim015 Oct 30, 2020
f4784e5
add Home Page, navbar, logout
abrarnaim015 Oct 31, 2020
ffb3e9e
add forn add todo, show all todo, dll
abrarnaim015 Oct 31, 2020
1fd1e9c
todo list done proces google sign in
abrarnaim015 Nov 1, 2020
cb63d74
proces google sign in
abrarnaim015 Nov 1, 2020
a6c5d9e
end
abrarnaim015 Nov 3, 2020
e652ac9
end
abrarnaim015 Nov 3, 2020
61d5994
process heroku
abrarnaim015 Nov 3, 2020
159b610
process heroku & forebase
abrarnaim015 Nov 3, 2020
56555ce
process heroku & forebase
abrarnaim015 Nov 3, 2020
1d73fae
Todo v1.1
abrarnaim015 Nov 6, 2020
255f746
fixed all bismillah
abrarnaim015 Nov 19, 2020
89a3884
fixed all bismillah and update heroku and firebase
abrarnaim015 Nov 20, 2020
507d299
fixed button google, layout, and progres responsiv
abrarnaim015 Jan 31, 2021
5b0b461
fixed button google, layout, and progres responsiv
abrarnaim015 Jan 31, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
532 changes: 531 additions & 1 deletion README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=3000
RAHASIA='nami<==**'
SALT=10
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
16 changes: 16 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('dotenv').config()

const express = require('express')
const app = express()
const port = process.env.PORT
const routers = require('./routes')
const errorHandler = require('./middlewares/errorHandler.js')

app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(routers)
app.use(errorHandler)

app.listen(port, ()=>{
console.log(`Let's Go to app FancyTodo http://localhost:${port}`)
})
23 changes: 23 additions & 0 deletions server/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "FancyTodo",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": null,
"database": "FancyTodo",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
113 changes: 113 additions & 0 deletions server/controller/todo-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const {Todo} = require ('../models')
const Super = require('../helper/super.js')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nama class nya jangan Super ya, mungkin InputValidation lebih cocok


class TodoController{

static async getTodo(req, res, next){
try {
let data = await Todo.findAll({
where: {UserId: req.key.id}
})
res.status(200).json(data)
} catch (err) {
next(err)
}
}

static async postTodo(req, res, next){
try {
let dataInput = req.body
let validasiError = Super.validasiPutTodo(dataInput)
if(validasiError.length>0){
let error ={
cek: 'postTodo',
status: 400,
msg: validasiError,
}
next(error)
}else{
dataInput.UserId = req.key.id
let data = await Todo.create(dataInput)
res.status(201).json(data)
}
} catch (err) {
next(err)
}
}

static async getTodoById(req, res, next){
try {
let id = +req.params.id
let data = await Todo.findByPk(id)
res.status(200).json(data)
} catch (err) {
next(err)
}
}

static async putTodo(req, res, next){
try {
let id = +req.params.id
let dataInput = req.body

let validasiError = Super.validasiPutTodo(dataInput)
if(validasiError.length>0){
let error ={
cek: 'postTodo',
status: 400,
msg: validasiError,
}
next(error)
}else{
if(!dataInput.status){
dataInput.status = 'not done'
}
let data = await Todo.update(dataInput, {
where: {id}
})
res.status(200).json(dataInput)
}
} catch (err) {
next(err)
}
}

static async patchTodo(req, res, next){
try {
let id = +req.params.id
let dataInput = req.body.status
let data = Todo.findByPk(id)
data.status = dataInput
Todo.update(data, {
where: {id}
})
res.status(200).json(data)
} catch (err) {
next(err)
}
}

static async deleteTodo(req, res, next){
try {
let id = +req.params.id
let data = await Todo.destroy({
where: {id}
})
if(data===0){
let errMess = {
massage: 'Id Tidak ditemukan'
}
res.status(400).json(errMess)
}else{
let output = {
massage: 'todo succes to delete'
}
res.status(200).json(output)
}
} catch (err) {
next(err)
}
}
}

module.exports = TodoController
52 changes: 52 additions & 0 deletions server/controller/user-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const {User} = require('../models')
const Super = require('../helper/super.js')

class UserController{
static async register(req, res){
try {
let dataBody = req.body
console.log(dataBody, 'benar' )
let newData = Super.validasiRegister(dataBody.password)
let dataUpuser = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

penamaan variabel dibenahi ya, misal newUser

email: dataBody.email,
password: newData.password
}
let data = await User.create(dataUpuser)
let postData = {
id: data.id,
email: data.email
}
res.status(201).json(postData)
} catch (err) {
console.log(err, 'err')
res.status(400).json(err)
}
}

static async login(req, res){
try {
let dataBody = req.body
let dataBaseUsers = await User.findOne({
where: {email: dataBody.email}
})
if(!dataBaseUsers){
res.status(401).json({error: 'Email atau Password anda tidak Valid 1'})
}else if(!Super.validasiLoginUser(dataBody.password, dataBaseUsers.password)){
res.status(401).json({error: 'Email atau Password anda tidak Valid 2'})
}else{
let dataUser = {
id: dataBaseUsers.id,
email: dataBaseUsers.email
}
let userToken = Super.tokenUser(dataUser)
dataUser.token = userToken
res.status(200).json(dataUser)
}
} catch (err) {
res.status(400).json(err)
}
}
}


module.exports = UserController
11 changes: 11 additions & 0 deletions server/ctt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[email protected]
abrarnaim

[email protected]
nami

[email protected]
nana

[email protected]
yuke
46 changes: 46 additions & 0 deletions server/helper/super.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken')

class Super {

static validasiPutTodo(data){
let dataEror = ''
for(let i in data){
if(i !== 'status'){
if(!data[i]){
let output = `${i} Tidak boleh kosong - `
dataEror += output
}
}
}

return dataEror
}

static validasiRegister(data){
const salt = bcrypt.genSaltSync(+process.env.SALT);
const hash = bcrypt.hashSync(data, salt);

return {
password: hash
}
}

static validasiLoginUser(passLogin, passDB){
return bcrypt.compareSync(passLogin, passDB)
}

static tokenUser(data){
const token = jwt.sign(data, process.env.RAHASIA)
return token
}

static cekToken(data){
var decoded = jwt.verify(data, process.env.RAHASIA);
return decoded
// console.log(decoded.foo)
}

}

module.exports = Super
26 changes: 26 additions & 0 deletions server/middlewares/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Super = require('../helper/super.js')
const {User} = require('../models')

const authentication = async (req, res, next)=>{
// console.log(req.headers)
let token = req.headers.token
try {
if(!token){
throw({msg: 'invalid token', status: 401})
}else{
let cekToken = Super.cekToken(token)
let dataUserDB = await User.findByPk(cekToken.id)

if(!dataUserDB){
throw({msg: 'invalid token', status: 401})
}else{
req.key = dataUserDB
next()
}
}
} catch (err) {
res.status(400).json(err)
}
}

module.exports = authentication
26 changes: 26 additions & 0 deletions server/middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {Todo} = require('../models')

const authorization = async (req, res, next)=>{
try {
let dataTodo = await Todo.findOne({
where: {
id: +req.params.id
}
})
if(dataTodo.UserId===req.key.id){
next()
}else{
throw({msg: 'Unauthorized', status: 401})
}
} catch (err) {
// let error ={
// name: 'suthorization',
// status: 400,
// msg: 'Unauthorized'
// }
next(err)
}

}

module.exports = authorization
13 changes: 13 additions & 0 deletions server/middlewares/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = (err, req, res, next) =>{
let status
let msg
if(!err.cek){
status = 500
msg = 'Internal Server Error, try again leter'
}else{
status = err.status
msg = err.msg
}

res.status(status).json({msg})
}
36 changes: 36 additions & 0 deletions server/migrations/20201026063326-create-todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Todos', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
status: {
type: Sequelize.STRING
},
dueDate: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Todos');
}
};
Loading