-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fforres
committed
Jul 11, 2020
1 parent
139ee02
commit 12a6064
Showing
14 changed files
with
580 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
{ | ||
"presets": ["next/babel"] | ||
"presets": ["next/babel"], | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-proposal-decorators", | ||
{ | ||
"legacy": true | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import bcrypt from 'bcryptjs' | ||
import jwt from 'jsonwebtoken' | ||
import { config } from '../../src/config' | ||
import { dbConnection } from '../../src/databaseConnection' | ||
import { UserEntity } from '../../src/models/user' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
res.statusCode = 200 | ||
const password = req.body?.password | ||
const email = req.body?.email | ||
console.log(req.body) | ||
if (!password || !email) { | ||
res.json({ error: true, errorMessage: 'missing body params' }) | ||
return | ||
} | ||
console.log('creating connection') | ||
const connection = await dbConnection() | ||
console.log('getting repository') | ||
const repository = connection.getRepository(UserEntity) | ||
console.log('find user') | ||
const user = await repository.findOne({ | ||
email, | ||
}) | ||
const passwordIsValid = bcrypt.compareSync(password, user.password) | ||
if (!passwordIsValid) { | ||
res.statusCode = 401 | ||
res.json({ error: true, errorMessage: 'invalid password' }) | ||
return | ||
} | ||
const { id } = user | ||
const token = jwt.sign({ id }, config.secret, { | ||
expiresIn: 86400, // expires in 24 hours | ||
}) | ||
res.json({ id, token }) | ||
} | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import jwt from 'jsonwebtoken' | ||
import bcrypt from 'bcryptjs' | ||
import { config } from '../../src/config' | ||
import { UserEntity } from '../../src/models/user' | ||
import { dbConnection } from '../../src/databaseConnection' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
res.statusCode = 200 | ||
const password = req.body?.password | ||
const email = req.body?.email | ||
console.log(req.body) | ||
|
||
if (!password || !email) { | ||
res.statusCode = 200 | ||
res.json({ error: true, errorMessage: 'missing body params' }) | ||
return | ||
} | ||
|
||
const hashedPassword = bcrypt.hashSync(req.body.password, 8) | ||
console.log('creating connection') | ||
const connection = await dbConnection() | ||
console.log('getting repository') | ||
const repository = connection.getRepository(UserEntity) | ||
const user = new UserEntity() | ||
user.email = email | ||
user.password = hashedPassword | ||
console.log('save user') | ||
const { id } = await repository.save(user) | ||
const token = jwt.sign({ id }, config.secret, { | ||
expiresIn: 86400, // expires in 24 hours | ||
}) | ||
res.json({ id, token }) | ||
} | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const config = { | ||
secret: 'necessitatibus quas repellat', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { UserEntity } from './models/user' | ||
import { createConnection, Connection } from 'typeorm' | ||
|
||
let connection: Connection = null | ||
export const dbConnection = async () => { | ||
if (!connection) { | ||
connection = await createConnection({ | ||
type: 'postgres', | ||
host: 'motty.db.elephantsql.com', | ||
port: 5432, | ||
username: 'ahqlymsk', | ||
password: 'P2LxGvaBzEucURU5An198BoGuF6XxZ6L', | ||
database: 'ahqlymsk', | ||
entities: [UserEntity], | ||
synchronize: true, | ||
logging: false, | ||
}) | ||
} | ||
return connection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
CreateDateColumn, | ||
DeleteDateColumn, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm' | ||
|
||
export abstract class BaseEntity { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string | ||
|
||
@CreateDateColumn({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' }) | ||
createDateTime: Date | ||
|
||
@UpdateDateColumn({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' }) | ||
lastChangedDateTime: Date | ||
|
||
@DeleteDateColumn({ type: 'timestamptz' }) | ||
inactiveDateTime: Date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Entity, Column } from 'typeorm' | ||
import { BaseEntity } from './base' | ||
|
||
@Entity() | ||
export class FavoritesEntity extends BaseEntity { | ||
@Column('varchar', { length: 500, unique: false, nullable: false }) | ||
pokeArray: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Entity, Column } from 'typeorm' | ||
import { BaseEntity } from './base' | ||
|
||
@Entity() | ||
export class UserEntity extends BaseEntity { | ||
@Column('varchar', { length: 500, unique: true, nullable: false }) | ||
email: string | ||
|
||
@Column('varchar', { length: 500, nullable: false }) | ||
password: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.