Skip to content

Commit 572fbeb

Browse files
committed
More development
1 parent 185eac1 commit 572fbeb

File tree

4 files changed

+27
-47
lines changed

4 files changed

+27
-47
lines changed

ApiTesting.http

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
###################################################################################
2+
###### CREATE ACCOUNT ######
3+
###################################################################################
4+
### Send POST request with json body
5+
POST http://localhost:3000/auth/create-account
6+
Content-Type: application/json
7+
8+
{
9+
"name": "Test User",
10+
"email": "[email protected]",
11+
"password": "toohardpasswd"
12+
}
13+
14+
###

src/components/Authentication/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import HttpError from "../../server/error";
44
import * as jwt from 'jsonwebtoken';
55
import {UserModel, UserModelInterface} from "../../database/models/user";
66
import * as User from '../../database/dao/user'
7+
import * as crypto from 'crypto';
8+
import * as bcrypt from 'bcrypt';
79

810
/**
911
* @export
@@ -16,12 +18,13 @@ export async function createAccount(req: Request, res: Response, next: NextFunct
1618
try {
1719

1820
const user = <UserModelInterface>{
21+
name: req.body.name,
1922
email: req.body.email,
2023
password: req.body.password
2124
};
2225

2326
// Check for mandatory details
24-
if (user.email === undefined || user.password === undefined) {
27+
if (user.name === undefined || user.email === undefined || user.password === undefined) {
2528
throw new Error('Missing details!');
2629
}
2730

@@ -32,6 +35,13 @@ export async function createAccount(req: Request, res: Response, next: NextFunct
3235
throw new Error('This email already exists');
3336
}
3437

38+
39+
// Hash password before database insert
40+
const salt: string = await bcrypt.genSalt(10);
41+
const hash: string = await bcrypt.hash(user.password, salt);
42+
user.password = hash;
43+
44+
3545
// This will insert new user into database
3646
const saved: UserModelInterface = await User.create(user);
3747
console.log('new user created: ' + saved);
@@ -43,7 +53,6 @@ export async function createAccount(req: Request, res: Response, next: NextFunct
4353

4454
res.json({
4555
status: 200,
46-
logged: true,
4756
token: token,
4857
message: 'Account created successfully'
4958
});

src/database/models/user.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ import sequelize from './index'
44
export class UserModel extends Model {
55
}
66

7-
/*
8-
export class AppUserModel {
9-
id: string;
10-
name: string;
11-
email: string;
12-
password: string;
13-
createdAt: Date;
14-
updatedAt: Date;
15-
}
16-
*/
17-
187
UserModel.init(
198
{
209
id: {
@@ -24,9 +13,9 @@ UserModel.init(
2413
},
2514
name: STRING(50),
2615
email: STRING(50),
27-
password: STRING(50)
16+
password: STRING(100)
2817
},
29-
{sequelize, modelName: 'UserModel'}
18+
{sequelize, modelName: 'User'}
3019
);
3120

3221

src/server/server.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,6 @@ app.set('secret', process.env.JWT_SECRET || '8fa410b58360fd97f3e15a752fd2d6281fc
4040
*/
4141
app.set('jwt-expire', process.env.JWT_EXPIRE || '60m');
4242

43-
/**
44-
* Set database dialect (postgres, mariaDb, sqlite..)
45-
*/
46-
app.set('db-dialect', process.env.DB_DIALECT || 'postgres');
47-
48-
49-
/**
50-
* Set database host address
51-
*/
52-
app.set('db-host', process.env.DB_HOST || '127.0.0.1');
53-
54-
/**
55-
* Set database host address
56-
*/
57-
app.set('db-port', process.env.DB_PORT || '5432');
58-
59-
/**
60-
* Set database user
61-
*/
62-
app.set('db-user', process.env.DB_USER || 'postgres');
63-
64-
/**
65-
* Set database password
66-
*/
67-
app.set('db-password', process.env.DB_PASSWD || 'BadPassword');
68-
69-
/**
70-
* Set database name
71-
*/
72-
app.set('db-database', process.env.DB_DATABASE || 'ApiExample');
73-
74-
7543
/**
7644
* @exports {express.Application}
7745
*/

0 commit comments

Comments
 (0)