Commit aaea74a2 authored by Geovanny's avatar Geovanny

Login/register api added.

parent 64e546a6
This diff is collapsed.
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"name": "cb-server", "name": "cb-server",
"dependencies": { "dependencies": {
"@koa/router": "^8.0.8", "@koa/router": "^8.0.8",
"bcrypt": "^4.0.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"fs": "0.0.1-security", "fs": "0.0.1-security",
"koa": "^2.11.0", "koa": "^2.11.0",
......
...@@ -129,4 +129,21 @@ userModel.createUserWithDiscord = async (discord_id) =>{ ...@@ -129,4 +129,21 @@ userModel.createUserWithDiscord = async (discord_id) =>{
await db.con.query(sql_text, [discord_id]); await db.con.query(sql_text, [discord_id]);
} }
userModel.registerUser = async (username, password) =>{
const hashPassword = await crypto.hash(password);
console.log(username, hashPassword)
const sql_text = 'INSERT INTO users (username, password) VALUES (?, ?)';
await db.con.query(sql_text, [username, hashPassword])
}
userModel.loginUser = async (username, password) =>{
const hashPassword = await crypto.hash(password);
console.log(username, hashPassword)
const sql_text = 'SELECT id, username from users WHERE username = ? AND password = ?';
const data = await db.con.query(sql_text, [username, hashPassword]);
return data[0];
}
module.exports = userModel; module.exports = userModel;
\ No newline at end of file
...@@ -29,6 +29,44 @@ router.post('/d-login', async (context, next) =>{ ...@@ -29,6 +29,44 @@ router.post('/d-login', async (context, next) =>{
} }
}); });
router.post('/login', async (context, next) =>{
if(context.session.user_id && userModel.getUserFromId(context.session.user_id)){
context.throw(400, 'User is Already Logged In')
}
const body = context.request.body;
if(!body || !body.username || !body.password){
context.throw(422, 'Missing parameters');
}
try{
const user = await userModel.loginUser(body.username, body.password);
if(!user){
throw Error('No user found')
}
console.log(user);
context.session.user_id = user.id;
context.response.body = {username: user.username};
context.status = 200;
}catch(error){
console.log(error);
context.throw(422, 'Login Failed')
}
});
router.post('/register', async(context, next)=>{
const body = context.request.body;
if(!body || !body.username || !body.password){
context.throw(422, 'Missing parameters');
}
try{
await userModel.registerUser(body.username, body.password);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Failed to register user')
}
})
router.post('/discord-register', async(context, next) =>{ router.post('/discord-register', async(context, next) =>{
const body = context.request.body; const body = context.request.body;
......
'use strict' 'use strict'
const bcrypt = require('bcrypt');
const ENV = require('../settings');
const crypto = {}; const crypto = {};
// """Someday, I will implement encryption and decryption. Today is not that day""" // """Someday, I will implement encryption and decryption. Today is not that day"""
crypto.encode = (data) =>{ crypto.hash = async (data) =>{
const buff = new Buffer(data); const hash = await new Promise((resolve, reject) => {
return buff.toString('base64'); bcrypt.hash(data, ENV.PASSWORD_SALT, function(err, hash) {
} if (err) reject(err)
crypto.decode = (enc_data) =>{ resolve(hash)
const buff = new Buffer(enc_data, 'base64'); });
return buff.toString('utf-8'); })
return hash;
} }
module.exports = crypto; module.exports = crypto;
\ No newline at end of file
...@@ -11,13 +11,13 @@ class LoginPageController{ ...@@ -11,13 +11,13 @@ class LoginPageController{
async login(credentials){ async login(credentials){
try{ try{
console.log(credentials)
if(credentials.username==="" || credentials.password===""){ if(credentials.username==="" || credentials.password===""){
alert("Fields can't be empty") alert("Fields can't be empty")
} }
// const user_data = await this.sync.login(credentials); const user_data = await this.sync.login(credentials);
// localStorage.setItem('username', user_data.user_name); console.log(user_data)
// location.replace('/'); localStorage.setItem('username', user_data.username);
location.replace('/');
}catch(error){ }catch(error){
console.log(error); console.log(error);
alert('Failed to login') alert('Failed to login')
...@@ -26,7 +26,6 @@ class LoginPageController{ ...@@ -26,7 +26,6 @@ class LoginPageController{
async register(credentials){ async register(credentials){
try{ try{
console.log(credentials)
if(credentials.username==="" || credentials.password==="" || credentials.confirm_password===""){ if(credentials.username==="" || credentials.password==="" || credentials.confirm_password===""){
alert("Fields can't be empty") alert("Fields can't be empty")
return; return;
...@@ -35,9 +34,9 @@ class LoginPageController{ ...@@ -35,9 +34,9 @@ class LoginPageController{
alert("Passwords don't match") alert("Passwords don't match")
return; return;
} }
// const user_data = await this.sync.register(credentials); await this.sync.register({username: credentials.username, password: credentials.password});
// localStorage.setItem('username', user_data.user_name); alert('Register Successful')
// location.replace('/'); location.reload();
}catch(error){ }catch(error){
console.log(error); console.log(error);
alert('Failed to register') alert('Failed to register')
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
<input type="text" name="username" placeholder="Geo"/> <input type="text" name="username" placeholder="Geo"/>
<input type="password" name="password" placeholder="********"/> <input type="password" name="password" placeholder="********"/>
<input type="password" name="confirm_password" placeholder="********"/> <input type="password" name="confirm_password" placeholder="********"/>
<button id="register_button">Log In</button> <button id="register_button">Register</button>
</register-form> </register-form>
</div> </div>
</center-div> </center-div>
......
class RegisterView extends EventTarget { class RegisterView extends EventTarget {
constructor(element){ constructor(element){
super() super()
this.element = element this.element = element
this.username_field = this.element.querySelector("[name=username]") this.username_field = this.element.querySelector("[name=username]")
this.password_field = this.element.querySelector("[name=password]") this.password_field = this.element.querySelector("[name=password]")
this.login_button = this.element.querySelector("#register_button") this.confirm_password_field = this.element.querySelector("[name=confirm_password]");
this.login_button = this.element.querySelector("#register_button")
this.login_button.addEventListener("click", () => { this.login_button.addEventListener("click", () => {
this.dispatchEvent(new CustomEvent("register_attempt", {detail: { this.dispatchEvent(new CustomEvent("register_attempt", {detail: {
username: this.username_field.value, username: this.username_field.value,
password: this.password_field.value } password: this.password_field.value,
})) confirm_password: this.confirm_password_field.value
}}))
}) })
} }
......
...@@ -26,9 +26,6 @@ class Sync{ ...@@ -26,9 +26,6 @@ class Sync{
if(!register_response.ok){ if(!register_response.ok){
throw new Error(`Register failed with ${login_response.status}`) throw new Error(`Register failed with ${login_response.status}`)
} }
const user_data = await register_response.json();
return user_data;
} }
} }
......
...@@ -20,6 +20,13 @@ ...@@ -20,6 +20,13 @@
</bar-options> </bar-options>
<bar-user> <bar-user>
<a href="/login">Login/Register</a> <a href="/login">Login/Register</a>
<div class="dropdown" style="display: none;">
<p>House</p>
<div class="dropdown-content">
<a>Profile</a>
<a onclick="logOut()">Log Out</a>
</div>
</div>
</bar-user> </bar-user>
</nav-bar> </nav-bar>
<script src="navbar.js"></script> <script src="navbar.js"></script>
\ No newline at end of file
...@@ -7,11 +7,23 @@ function loadPage(href) ...@@ -7,11 +7,23 @@ function loadPage(href)
} }
const nav_placeholder = document.querySelector('nav-placeholder') const nav_placeholder = document.querySelector('nav-placeholder')
nav_placeholder.innerHTML = loadPage('/navbar/navbar.html') nav_placeholder.innerHTML = loadPage('/navbar/navbar.html')
const bar_user = document.querySelector('bar-user a');
const saved_user = localStorage.getItem('username'); const saved_user = localStorage.getItem('username');
if(saved_user){ if(saved_user){
bar_user.innerText = saved_user; const bar_user_default = document.querySelector('bar-user a');
const bar_user_dropdown = document.querySelector('bar-user .dropdown')
const bar_user_name = bar_user_dropdown.querySelector('p');
bar_user_default.style.display = "none";
bar_user_dropdown.style.display = "";
bar_user_name.innerText = saved_user;
} }
function testy(){
console.log('asd') function logOut(){
console.log(document.cookie);
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
var spcook = cookies[i].split("=");
document.cookie = spcook[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";
}
localStorage.removeItem('username');
location.reload();
} }
\ No newline at end of file
...@@ -66,4 +66,14 @@ bar-user{ ...@@ -66,4 +66,14 @@ bar-user{
bar-user:hover{ bar-user:hover{
background-color: rgb(95, 26, 26); background-color: rgb(95, 26, 26);
}
bar-user{
.dropdown{
margin-top: -30px;
}
.dropdown:hover{
background-color: rgb(95, 26, 26);;
}
} }
\ No newline at end of file
...@@ -92,6 +92,13 @@ bar-user:hover { ...@@ -92,6 +92,13 @@ bar-user:hover {
background-color: #5f1a1a; background-color: #5f1a1a;
} }
bar-user .dropdown {
margin-top: -30px;
}
bar-user .dropdown:hover {
background-color: #5f1a1a;
}
content-body { content-body {
grid-area: content; grid-area: content;
} }
......
{"version":3,"sourceRoot":"","sources":["../fontawesome/font_awesome.scss","../sass/navbar.scss","../sass/login.scss","../sass/main.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAmBF;EACI;IAAK;;EACP;IAAO;;;AC1CX;EACI;EACA;EACA;EACA,eACI;EAGJ;;;AAGJ;EAEI;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;;;AAIR;EACI;;AACA;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI;;;ACnEJ;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EAEA;;AAEA;EACI;EACA;;;AAIN;AACF;EACI;EAEA;EACA;EACA;EACA;EACA;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;EACA;EACA;EACA;;;AAGJ;EACI;AAA0B;;;AAG9B;AACA;EACI;IAAM;;EACN;IAAI;;;AC5DR;EACI;EACA;EACA;EACA;EACA,eACI","file":"main.css"} {"version":3,"sourceRoot":"","sources":["../fontawesome/font_awesome.scss","../sass/navbar.scss","../sass/login.scss","../sass/main.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAmBF;EACI;IAAK;;EACP;IAAO;;;AC1CX;EACI;EACA;EACA;EACA,eACI;EAGJ;;;AAGJ;EAEI;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;;;AAIR;EACI;;AACA;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI;;;AAKA;EACI;;AAEJ;EACI;;;AC5ER;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EAEA;;AAEA;EACI;EACA;;;AAIN;AACF;EACI;EAEA;EACA;EACA;EACA;EACA;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;EACA;EACA;EACA;;;AAGJ;EACI;AAA0B;;;AAG9B;AACA;EACI;IAAM;;EACN;IAAI;;;AC5DR;EACI;EACA;EACA;EACA;EACA,eACI","file":"main.css"}
\ No newline at end of file \ No newline at end of file
...@@ -63,4 +63,11 @@ bar-user:hover { ...@@ -63,4 +63,11 @@ bar-user:hover {
background-color: #5f1a1a; background-color: #5f1a1a;
} }
bar-user .dropdown {
margin-top: -30px;
}
bar-user .dropdown:hover {
background-color: #5f1a1a;
}
/*# sourceMappingURL=navbar.css.map */ /*# sourceMappingURL=navbar.css.map */
{"version":3,"sourceRoot":"","sources":["../sass/navbar.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA,eACI;EAGJ;;;AAGJ;EAEI;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;;;AAIR;EACI;;AACA;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI","file":"navbar.css"} {"version":3,"sourceRoot":"","sources":["../sass/navbar.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA,eACI;EAGJ;;;AAGJ;EAEI;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;;;AAIR;EACI;;AACA;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI;;;AAKA;EACI;;AAEJ;EACI","file":"navbar.css"}
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment