Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
fforres committed Jul 11, 2020
1 parent 139ee02 commit 12a6064
Show file tree
Hide file tree
Showing 14 changed files with 580 additions and 56 deletions.
10 changes: 9 additions & 1 deletion .babelrc
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
}
]
]
}
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
"plugin:@typescript-eslint/recommended",
// Uncomment the following lines to enable eslint-config-prettier
// Is not enabled right now to avoid issues with the Next.js repo
// "prettier",
// "prettier/@typescript-eslint"
"prettier",
"prettier/@typescript-eslint"
],
"env": {
"es6": true,
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@
]
},
"dependencies": {
"@babel/plugin-proposal-decorators": "^7.10.4",
"@types/pg": "^7.14.4",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^8.5.1",
"next": "latest",
"pg": "^8.3.0",
"react": "^16.13.1",
"react-dom": "^16.13.1"
"react-dom": "^16.13.1",
"typeorm": "^0.2.25"
},
"devDependencies": {
"@testing-library/react": "^10.0.1",
"@types/bcryptjs": "^2.4.2",
"@types/jest": "^25.1.4",
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^13.9.5",
"@types/react": "^16.9.27",
"@types/testing-library__react": "^10.0.0",
Expand Down
10 changes: 0 additions & 10 deletions pages/api/hello.tsx

This file was deleted.

38 changes: 38 additions & 0 deletions pages/api/login.tsx
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
36 changes: 36 additions & 0 deletions pages/api/signup.tsx
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
46 changes: 12 additions & 34 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export const Home = (): JSX.Element => (
</Head>

<main>
<h1 className="title">
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<h1 className="title">Welcome to poke login api</h1>

<p className="description">
Get started by editing <code>pages/index.tsx</code>
please goto{' '}
<a href="https://github.com/Noders/30diasdereact">30diasdereact</a>to
learn more
</p>

<button
Expand All @@ -25,41 +25,19 @@ export const Home = (): JSX.Element => (
</button>

<div className="grid">
<a href="https://nextjs.org/docs" className="card">
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className="card">
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/master/examples"
className="card"
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className="card"
>
<h3>Deploy &rarr;</h3>
<p>Instantly deploy your Next.js site to a public URL with Vercel.</p>
<a href="https://github.com/Noders/30diasdereact" className="card">
<h3>Uso</h3>
<p>
Este es un projecto armado para apoyar en el 30diasdereactchallenge
de Noders
</p>
</a>
</div>
</main>

<footer>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by <img src="/vercel.svg" alt="Vercel Logo" className="logo" />
<a href="https://noders.com" target="_blank" rel="noopener noreferrer">
Powered by Noders
</a>
</footer>

Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const config = {
secret: 'necessitatibus quas repellat',
}
20 changes: 20 additions & 0 deletions src/databaseConnection.ts
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
}
20 changes: 20 additions & 0 deletions src/models/base.ts
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
}
8 changes: 8 additions & 0 deletions src/models/favoritos.ts
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
}
11 changes: 11 additions & 0 deletions src/models/user.ts
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
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
Expand Down
Loading

0 comments on commit 12a6064

Please sign in to comment.