Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
'bakcend
Browse files Browse the repository at this point in the history
  • Loading branch information
andyruwruw committed Oct 3, 2020
1 parent 96e9b68 commit 4e9630f
Show file tree
Hide file tree
Showing 29 changed files with 473 additions and 201 deletions.
16 changes: 5 additions & 11 deletions api/check-user.ts
@@ -1,21 +1,15 @@
import { NowRequest, NowResponse } from '@vercel/node';
import { connect, connection } from 'mongoose';
import { connection } from 'mongoose';

import { verifyToken } from '../utils/auth';

const {
MONGO_USER,
MONGO_PASSWORD,
} = process.env;
import { connectDB } from '../utils/db';
connectDB();

connect(`mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@cluster0.oj6kf.mongodb.net/proactive?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
import { verifyToken } from '../utils/auth';

export default async function (req: NowRequest, res: NowResponse) {
try {
let user = await verifyToken(req);

connection.close();
return res.send(user);
} catch (error) {
Expand Down
31 changes: 0 additions & 31 deletions api/get-item.ts

This file was deleted.

23 changes: 13 additions & 10 deletions api/get-items.ts
@@ -1,16 +1,19 @@
import { NowRequest, NowResponse } from '@vercel/node';
import { connect } from 'mongoose';
import { connection } from 'mongoose';

const {
MONGO_USER,
MONGO_PASSWORD,
} = process.env;
import { connectDB } from '../utils/db';
connectDB();

connect(`mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@cluster0.oj6kf.mongodb.net/proactive?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
import { Item } from '../models';
import { verifyToken } from '../utils/auth';

export default async function (req: NowRequest, res: NowResponse) {
return res.status(200).send('Hello');
let user = await verifyToken(req);

let items = await Item.find({
user,
});

connection.close();
return res.status(200).send(items);
}
21 changes: 6 additions & 15 deletions api/login.ts
@@ -1,25 +1,16 @@
import { NowRequest, NowResponse } from '@vercel/node';
import { connect, connection } from 'mongoose';
import { connection } from 'mongoose';

import * as models from '../models/index.js';
import { login } from '../utils/auth.js';
import { connectDB } from '../utils/db';
connectDB();

const User = models.default.User.default;

const {
MONGO_USER,
MONGO_PASSWORD,
} = process.env;

connect(`mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@cluster0.oj6kf.mongodb.net/proactive?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
import { User } from '../models';
import { login } from '../utils/auth';

export default async function (req: NowRequest, res: NowResponse) {
try {
const existingUser = await User.findOne({
username: req.body.username
username: req.body.username,
});

if (!existingUser || !await existingUser.comparePassword(req.body.password)) {
Expand Down
32 changes: 32 additions & 0 deletions api/register.ts
@@ -0,0 +1,32 @@
import { NowRequest, NowResponse } from '@vercel/node';
import { connection } from 'mongoose';

import { connectDB } from '../utils/db';
connectDB();

import { User } from '../models';
import { register } from '../utils/auth';

export default async function (req: NowRequest, res: NowResponse) {
try {
const existingUser = await User.findOne({
username: req.body.username
});

if (existingUser) {
return res.status(400).send('Username taken');
}

let user = new User({
username: req.body.username,
password: req.body.password,
});

await user.save();

return await register(user, res, connection);
} catch (error) {
connection.close();
return res.status(502).send(error);
}
}
41 changes: 0 additions & 41 deletions api/sign-up.ts

This file was deleted.

3 changes: 1 addition & 2 deletions models/Item.js
Expand Up @@ -46,5 +46,4 @@ const schema = new mongoose.Schema({
},
});

const Item = mongoose.model('Item', schema);
export default Item;
export const Item = mongoose.model('Item', schema);
28 changes: 3 additions & 25 deletions models/User.js
Expand Up @@ -4,7 +4,7 @@ const bcrypt = require('bcrypt');
const { removeOldTokens } = require('../utils/auth');

const {
SERVER_SECRET
SALT_ROUNDS,
} = process.env;

const schema = new mongoose.Schema({
Expand All @@ -16,10 +16,6 @@ const schema = new mongoose.Schema({
type: String,
required: true,
},
name: {
type: String,
required: true,
},
tokens: [],
});

Expand All @@ -29,8 +25,7 @@ schema.pre('save', async function(next) {
}

try {
const salt = await bcrypt.genSalt(SERVER_SECRET);
const hash = await bcrypt.hash(this.password, salt);
const hash = await bcrypt.hashSync(this.password, parseInt(SALT_ROUNDS));

this.password = hash;
next();
Expand Down Expand Up @@ -68,21 +63,4 @@ schema.methods.removeOldTokens = function() {
this.tokens = removeOldTokens(this.tokens);
}

// // middleware to validate user account
// schema.statics.verify = async function(req, res, next) {
// // look up user account
// const user = await User.findOne({
// _id: req.user_id
// });
// if (!user || !user.tokens.includes(req.token))
// return res.clearCookie('token').status(403).send({
// error: "Invalid user account."
// });

// req.user = user;

// next();
// }

const User = mongoose.model('User', schema);
export default User;
export const User = mongoose.model('User', schema);
7 changes: 0 additions & 7 deletions models/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions models/index.ts
@@ -0,0 +1,2 @@
export { User } from './User';
export { Item } from './Item';
Binary file modified public/favicon.ico
Binary file not shown.
4 changes: 2 additions & 2 deletions public/index.html
Expand Up @@ -5,13 +5,13 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<title>Proactive</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css">
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
<strong>We're sorry but Proactive doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
Expand Down
11 changes: 8 additions & 3 deletions src/App.vue
@@ -1,21 +1,26 @@
<template>
<div class="content">
<div class="nav-container">
<nav-button image="home" />

<h1>
PROACTIVE
</h1>

<nav-button image="settings" />
</div>

<router-view />
</div>
</template>

<script>
import NavButton from '@/components/NavButton.vue';
export default {
name: 'App',
components: {
NavButton,
},
data: () => ({
Expand All @@ -35,7 +40,7 @@ html {
border-radius: 5px;
color: #384b5f;
font-family: 'Roboto Slab', serif;
max-width: 1248px;
max-width: 1246px;
margin: 0 auto;
margin-top: 10px;
min-height: calc(100vh - 20px);
Expand All @@ -57,7 +62,7 @@ html {
color: #516477;
font-weight: lighter;
margin-top: 15px;
opacity: .1;
opacity: .3;
text-shadow: 0px 0px 0px rgba(0, 0, 0, 0.61);
transition: all .2s ease;
}
Expand Down
20 changes: 20 additions & 0 deletions src/api/auth/index.js
@@ -0,0 +1,20 @@
import axios from 'axios';

export default {
async getUser() {
let response = await axios.get('/api/check-user');
return response.data;
},
async login(payload) {
let response = await axios.put('/api/login', payload);
return response.data;
},
async register(payload) {
let response = await axios.put('/api/register', payload);
return response.data;
},
async logout() {
let response = await axios.put('/api/logout');
return response.data;
},
};
7 changes: 7 additions & 0 deletions src/api/index.js
@@ -0,0 +1,7 @@
import auth from './auth';
import items from './items';

export default {
auth,
items,
};
14 changes: 14 additions & 0 deletions src/api/items/index.js
@@ -0,0 +1,14 @@
export default {
getItems() {
return [];
},
getItem() {
return 0;
},
editItem() {
return 0;
},
deleteItem() {
return 0;
},
};
1 change: 1 addition & 0 deletions src/assets/icons/home.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4e9630f

Please sign in to comment.