Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
cbdiscord
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Geovanny E. Vera Pazmino
cbdiscord
Commits
aaea74a2
Commit
aaea74a2
authored
May 13, 2020
by
Geovanny
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Login/register api added.
parent
64e546a6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
533 additions
and
32 deletions
+533
-32
package-lock.json
server/package-lock.json
+402
-0
package.json
server/package.json
+1
-0
model.js
server/user/model.js
+17
-0
route.js
server/user/route.js
+38
-0
crypto.js
server/util/crypto.js
+10
-8
controller.js
web/login/controller.js
+7
-8
index.html
web/login/index.html
+1
-1
register_view.js
web/login/register_view.js
+8
-6
sync.js
web/login/sync.js
+0
-3
navbar.html
web/navbar/navbar.html
+7
-0
navbar.js
web/navbar/navbar.js
+16
-4
navbar.scss
web/sass/navbar.scss
+10
-0
main.css
web/stylesheets/main.css
+7
-0
main.css.map
web/stylesheets/main.css.map
+1
-1
navbar.css
web/stylesheets/navbar.css
+7
-0
navbar.css.map
web/stylesheets/navbar.css.map
+1
-1
No files found.
server/package-lock.json
View file @
aaea74a2
This diff is collapsed.
Click to expand it.
server/package.json
View file @
aaea74a2
...
@@ -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"
,
...
...
server/user/model.js
View file @
aaea74a2
...
@@ -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
server/user/route.js
View file @
aaea74a2
...
@@ -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
;
...
...
server/util/crypto.js
View file @
aaea74a2
'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
web/login/controller.js
View file @
aaea74a2
...
@@ -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'
)
...
...
web/login/index.html
View file @
aaea74a2
...
@@ -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>
...
...
web/login/register_view.js
View file @
aaea74a2
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
}}))
})
})
}
}
...
...
web/login/sync.js
View file @
aaea74a2
...
@@ -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
;
}
}
}
}
...
...
web/navbar/navbar.html
View file @
aaea74a2
...
@@ -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
web/navbar/navbar.js
View file @
aaea74a2
...
@@ -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
web/sass/navbar.scss
View file @
aaea74a2
...
@@ -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
web/stylesheets/main.css
View file @
aaea74a2
...
@@ -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
;
}
}
...
...
web/stylesheets/main.css.map
View file @
aaea74a2
{"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
web/stylesheets/navbar.css
View file @
aaea74a2
...
@@ -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 */
web/stylesheets/navbar.css.map
View file @
aaea74a2
{"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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment