a
This commit is contained in:
parent
1ed59aba97
commit
2ecb69c828
@ -64,7 +64,6 @@ const apikeyModel = sequelize.define(
|
||||
|
||||
module.exports = { apikeyModel };
|
||||
|
||||
|
||||
/*
|
||||
class AuthToken extends Model {
|
||||
check(){
|
||||
|
@ -4,12 +4,9 @@ const { userModel } = require("../database/model/userModel.js");
|
||||
const { Op, Sequelize } = require("sequelize");
|
||||
const { hashAPIKey } = require("../functions/bcrypt.js");
|
||||
const { generateUUID } = require("../functions/generateUUID.js");
|
||||
const { hashPassword , hashAPIKey } = require("../functions/bcrypt.js");
|
||||
const { hashPassword , comparePassword , hashAPIKey } = require("../functions/bcrypt.js");
|
||||
|
||||
|
||||
async function getUser() {
|
||||
const user = await userModel.findAll();
|
||||
return user;
|
||||
}
|
||||
//api/v0/user/register
|
||||
|
||||
/* Registering new user
|
||||
@ -31,6 +28,10 @@ async function addUser(user) {
|
||||
});
|
||||
}
|
||||
|
||||
async function getAPIKey() {
|
||||
const apikey = await apikeyModel.findAll();
|
||||
return apikey;
|
||||
}
|
||||
|
||||
/*
|
||||
1) take userid
|
||||
@ -40,8 +41,6 @@ async function addUser(user) {
|
||||
5) you give the user rowid-uuidv4
|
||||
6) store in database
|
||||
*/
|
||||
|
||||
|
||||
async function addAPIKey(userId, permission) {
|
||||
let token = await generateUUID();
|
||||
let usertoken = userId + "-" + token;
|
||||
@ -62,8 +61,7 @@ async function addAPIKey(userId, permission) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUser,
|
||||
addUser,
|
||||
getAPIKey,
|
||||
loginUser,
|
||||
addAPIKey,
|
||||
};
|
||||
|
@ -29,9 +29,14 @@ async function hashAPIKey(apikey) {
|
||||
return await bcrypt.hash(apikey, saltRounds);
|
||||
}
|
||||
|
||||
async function comparePassword(password, hash) {
|
||||
return await bcrypt.compare(password, hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
hashAPIKey,
|
||||
comparePassword
|
||||
};
|
@ -0,0 +1,4 @@
|
||||
/*v
|
||||
1) check if token proided by JSON req is valid against db
|
||||
2) if valid its passed to next()
|
||||
*/
|
@ -153,6 +153,14 @@ app.auth = (function (app) {
|
||||
localStorage.setItem("APIToken", token);
|
||||
}
|
||||
|
||||
function setUserId(userId) {
|
||||
localStorage.setItem("userId", userId);
|
||||
}
|
||||
|
||||
function setUsername(username) {
|
||||
localStorage.setItem("username", username);
|
||||
}
|
||||
|
||||
function getToken() {
|
||||
return localStorage.getItem("APIToken");
|
||||
}
|
||||
@ -167,7 +175,7 @@ app.auth = (function (app) {
|
||||
callback(null, false);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
function logIn(args, callback) {
|
||||
app.api.post("auth/login", args, function (error, data) {
|
||||
if (data.login) {
|
||||
@ -176,9 +184,14 @@ app.auth = (function (app) {
|
||||
callback(error, !!data.token);
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
function logOut(callback) {
|
||||
localStorage.removeItem("APIToken");
|
||||
localStorage.removeItem("userId");
|
||||
localStorage.removeItem("username");
|
||||
|
||||
//remove token from db NOT the api key.
|
||||
callback();
|
||||
}
|
||||
|
||||
@ -196,17 +209,24 @@ app.auth = (function (app) {
|
||||
|
||||
function logInRedirect() {
|
||||
window.location.href =
|
||||
//window.location.href = location.href.replace(location.origin+'/login', '') || '/'
|
||||
location.href.replace(location.replace(`/login`)) || "/";
|
||||
}
|
||||
|
||||
function homeRedirect(){
|
||||
window.location.href =
|
||||
location.href.replace(location.replace(`/`)) || "/";
|
||||
}
|
||||
|
||||
return {
|
||||
getToken: getToken,
|
||||
setToken: setToken,
|
||||
isLoggedIn: isLoggedIn,
|
||||
logIn: logIn,
|
||||
//logIn: logIn,
|
||||
logOut: logOut,
|
||||
forceLogin,
|
||||
logInRedirect,
|
||||
homeRedirect,
|
||||
};
|
||||
})(app);
|
||||
|
||||
|
@ -51,7 +51,7 @@ router.get('/news', function(req, res, next) {
|
||||
res.render('news');
|
||||
});
|
||||
|
||||
//login / register page
|
||||
//login | register page
|
||||
router.get('/login', function(req, res, next) {
|
||||
res.render('signuplogin');
|
||||
});
|
||||
|
@ -1,19 +1,8 @@
|
||||
const { getUser, addUser } = require("../functions/apiDatabase.js");
|
||||
const { addUser , loginUser } = require("../functions/apiDatabase.js");
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
//get all users
|
||||
router.get("/", async (req, res, next) => {
|
||||
try {
|
||||
const location = await getUser();
|
||||
res.status(200).json(location);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// /user/register
|
||||
router.post("/register", async (req, res, next) => {
|
||||
@ -27,8 +16,19 @@ router.post("/register", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//login
|
||||
router.post("/login", async (req, res, next) => {
|
||||
try {
|
||||
console.log("this is " , req.body);
|
||||
let res = await loginUser(req.body);
|
||||
if (res == false){
|
||||
console.log("user not found");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
//update
|
||||
//delete
|
||||
//getbyid
|
||||
|
@ -5,11 +5,8 @@
|
||||
<div class="form signup" >
|
||||
<!--<div class="form signup card" -->
|
||||
<header>Signup</header>
|
||||
<!-- Return message from api -->
|
||||
<div class="actionMessage" style="display:none"></div>
|
||||
<!-- localhost/api/v0/user/register -->
|
||||
|
||||
<!-- evalAjax Fires when status is returned -->
|
||||
<!-- evalAjax Fires when status 200 is returned -->
|
||||
<form action="user/register" onsubmit="formAJAX(this)" evalAJAX="app.auth.logInRedirect();">
|
||||
<input type="text" name="username" placeholder="Username" required />
|
||||
<input type="text" name="email" placeholder="Email" required />
|
||||
@ -23,11 +20,12 @@
|
||||
|
||||
<div class="form login">
|
||||
<header>Login</header>
|
||||
<!-- Return message from api -->
|
||||
<div class="actionMessage" style="display:none"></div>
|
||||
<form action="user/login" onsubmit="formAJAX(this)" evalAJAX="app.auth.logInRedirect();">
|
||||
<input type="text" id="email" placeholder="Email address" required />
|
||||
<input type="password" id="password" placeholder="Password" required />
|
||||
<!-- evalAjax Fires when status 200 is returned -->
|
||||
<form action="user/login" onsubmit="formAJAX(this)"
|
||||
evalAJAX="app.auth.homeRedirect();"
|
||||
>
|
||||
<input type="text" name="userInfo" placeholder="Email address | Username" required />
|
||||
<input type="password" name="password" placeholder="Password" required />
|
||||
<a href="/resetPassword">Forgot password?</a>
|
||||
<input type="submit" value="Login" />
|
||||
</form>
|
||||
|
Loading…
x
Reference in New Issue
Block a user