Skip to content

Commit

Permalink
Login, register and account fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-0712 committed Jul 21, 2023
1 parent fe8701a commit 0791719
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 33 deletions.
23 changes: 23 additions & 0 deletions models/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const arr = [];
var currBlog = [];

module.exports = class newBlog
{
constructor(title, content)
{
this.title = title;
this.content = content;
this.id = Math.random().toString();
}

save()
{
const tmp = {TITLE:this.title, CONTENT:this.content, ID:this.id};
arr.push(tmp);
currBlog = [];
currBlog.push(tmp);
}

static getAllBLogs(){return arr};
static getLastBlog(){return currBlog};
}
39 changes: 25 additions & 14 deletions models/credentials.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const user_list = [];
const email_list = [];
const pass_list = [];
const blogIDs = [];

const arr = [];
var curr = [];
module.exports = class addProfile
{
constructor(user,email,pass)
Expand All @@ -14,18 +11,32 @@ module.exports = class addProfile

save()
{
user_list.push(this.user);
email_list.push(this.email);
pass_list.push(this.pass);
const tmp = {USERNAME:this.user, EMAIL:this.email, PASSWORD:this.pass, BLOGS:[]};
arr.push(tmp);
curr = [];
curr.push(tmp);
}

static saveCurrent(u,e,p,b)
{
const tmp = {USERNAME:u, EMAIL:e, PASSWORD:p, BLOGS:b};
curr = [];
curr.push(tmp);
}

saveBlog(id)
static saveBlog(id, user)
{
blogIDs.push(id);
let i=0;
for (i; i<arr.length; i++)
{
if (user == arr[i].USERNAME)
{
arr[i].BLOGS.push(id);
break;
}
}
}

static getUser(){return user_list};
static getEmail(){return email_list};
static getPass(){return pass_list};
static getBlogs(){return blogIDs};
static getAllUsers(){return arr};
static getCurrentUser(){return curr};
}
12 changes: 7 additions & 5 deletions routes/slash-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const addProfile = require('../models/credentials');
const route = exp.Router();

route.get('/account', (req,res) => {
const NAME = addProfile.getUser();
const EMAIL = addProfile.getEmail();
const PASS = addProfile.getPass();
const NO = addProfile.getBlogs();

const data = {NAME:NAME, EMAIL:EMAIL, PASS:PASS, NUM:NO.length};
const curr = addProfile.getCurrentUser();

const n = curr[0].USERNAME;
const e = curr[0].EMAIL;
const p = curr[0].PASSWORD;
const b = curr[0].BLOGS;

const data = {NAME:n, EMAIL:e, PASS:p, NUM:b.length};
res.render('account', data);
})

Expand Down
26 changes: 15 additions & 11 deletions routes/slash-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,32 @@ route.post('/login', (req,res) => {
var f1,f2;
f1=f2=0;

const user_list = addProfile.getUser();
const email_list = addProfile.getEmail();
const pass_list = addProfile.getPass();
const all = addProfile.getAllUsers();
const tmpPass = req.body.PASSWORD;

let i=0;
for (i; i<user_list.length; i++)
for (i; i<all.length; i++)
{
if(req.body.USER_EMAIL == user_list[i] || req.body.USER_EMAIL == email_list[i])
if(req.body.USER_EMAIL == all[i].USERNAME || req.body.USER_EMAIL == all[i].EMAIL)
{
f1=1;
if(tmpPass == all[i].PASSWORD)
{
f2=1;
}
break;
}
}

if(req.body.PASSWORD == pass_list[i])
{
f2=1;
}

if (f1==1 && f2==1)
{
res.sendFile(path.join(__dirname, '../', 'views', 'home.html'));
const u = all[i].USERNAME;
const e = all[i].EMAIL;
const p = all[i].PASSWORD;
const b = all[i].BLOGS;

addProfile.saveCurrent(u,e,p,b);
res.redirect('/home');
}
else
{
Expand Down
14 changes: 14 additions & 0 deletions routes/slash-post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
const exp = require('express');
const route = exp.Router();
const path = require('path');
const newBlog = require('../models/blog');
const addProfile = require('../models/credentials');

route.get('/post', (req,res) => {
res.sendFile(path.join(__dirname, '../', 'views', 'post.html'));
})

route.post('/post', (req,res) => {
const obj = new newBlog(req.body.TITLE, req.body.BLOG);
obj.save();

const latestBlog = newBlog.getLastBlog();

const currProfile = addProfile.getCurrentUser();

addProfile.saveBlog(latestBlog[0].ID, currProfile[0].USERNAME);
res.redirect('/home');
})

module.exports = route;
3 changes: 1 addition & 2 deletions routes/slash-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const exp = require('express');
const route = exp.Router();
const path = require('path');
const addProfile = require('../models/credentials');
const e = require('express');

const pattern = new RegExp('@gmail.com');

Expand Down Expand Up @@ -42,7 +41,7 @@ route.post('/register', (req,res) => {
{
const obj = new addProfile(uSeR, eMaIl, pAsS);
obj.save();
res.sendFile(path.join(__dirname, '../', 'views', 'home.html'));
res.redirect('/home');
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion views/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<img src="../images/main_logo.png" alt="logo">
<p>BlogBase</p>
</div>
<form action="/postBlog" method="post" autocomplete="off">
<form action="/post" method="post" autocomplete="off">
<p class="header">Post a New Blog</p>

<p class="label">Title</p>
Expand Down

0 comments on commit 0791719

Please sign in to comment.