Skip to content

Commit c36485e

Browse files
authored
Add files via upload
1 parent b6b4dbf commit c36485e

16 files changed

+4503
-0
lines changed

Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:16
2+
3+
WORKDIR /usr/src/smartbrainapi
4+
5+
COPY ./ ./
6+
7+
RUN npm install
8+
9+
CMD [ "/bin/bash" ]

controllers/authorization.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import redisClient from '../server.js';
2+
3+
const requireAuth=(req, res, next)=>{
4+
const {authorization}=req.headers;
5+
if(!authorization) {
6+
return res.status(401).json('Unauthorized');
7+
}
8+
return redisClient.get(authorization, (err, reply)=>{
9+
if(err || !reply){
10+
return res.status(401).json('Unauthorized')
11+
}
12+
console.log('you shall pass!');
13+
return next();
14+
})
15+
}
16+
17+
export default requireAuth;

controllers/image.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Clarifai from 'clarifai';
2+
3+
const app = new Clarifai.App({
4+
apiKey: '57f9910d75c54e98a69894833187303c'
5+
});
6+
7+
export const handleApiCall=(req, res)=>{
8+
app.models.predict(Clarifai.FACE_DETECT_MODEL, req.body.input)
9+
.then(data=>res.json(data))
10+
.catch(err=>res.status(400).json('unable to work with API'))
11+
}
12+
13+
14+
export const handleImage=(req, res, db)=>{
15+
const {id}=req.body;
16+
return db('users').where('id','=',id)
17+
.increment('entries', 1)
18+
.returning('entries')
19+
.then(entries=>res.json(entries[0].entries))
20+
.catch(err=>res.status(400).json('unable to get entries'))
21+
}

controllers/profile.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const handleProfileGet=(req, res, db)=>{
2+
const {id}=req.params;
3+
return db.select('*').from('users').where({id
4+
// id:id
5+
})
6+
.then(user=>{
7+
if(user.length){
8+
res.json(user[0]);
9+
} else{
10+
res.status(400).json('User not found');
11+
}
12+
})
13+
.catch(err=>res.status(400).json('error getting user'))
14+
}
15+
16+
export const handleProfileUpdate=(req, res, db)=>{
17+
const {id}= req.params;
18+
const {name, age, pet}=req.body.formInput;
19+
db('users').where({id}).update({name, age, pet})
20+
.then(resp=>{
21+
if(resp){
22+
res.json('success')
23+
}
24+
else{
25+
res.status(400).json('Unable to update')
26+
}
27+
})
28+
.catch(err=>res.status(400).json('error updating user'))
29+
}

controllers/register.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {getAuthTokenId, createSession } from './signin.js';
2+
3+
const handleRegister=(req, res, db, bcrypt)=>{
4+
const {name, email, password}=req.body;
5+
if(!email || !name || !password){
6+
return Promise.reject('incorrect form submission');
7+
}
8+
const hash = bcrypt.hashSync(password);
9+
10+
return db.transaction(trx =>{
11+
trx.insert({
12+
email:email,
13+
hash:hash
14+
})
15+
.into('login')
16+
.returning('email')
17+
.then(loginEmail=>{
18+
return trx('users')
19+
.returning('*')
20+
.insert({
21+
name:name,
22+
email:loginEmail[0].email,
23+
joined: new Date()
24+
})
25+
.then(response=> {
26+
return response[0]})
27+
})
28+
.then(trx.commit)
29+
.catch(trx.rollback)
30+
})
31+
.catch(err=>Promise.reject('unable to register'))
32+
}
33+
34+
const register=(db, bcrypt)=>(req, res)=>{
35+
const {authorization} =req.headers;
36+
return authorization? getAuthTokenId(req, res) :
37+
handleRegister(req, res, db, bcrypt)
38+
.then (data=>{
39+
return data.id && data.email ? createSession(data) : Promise.reject(data)
40+
})
41+
.then (session=> {
42+
res.json(session)})
43+
.catch(err=>res.status(400).json(err))
44+
}
45+
46+
export default register;

controllers/signin.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import jwt from "jsonwebtoken";
2+
import redisClient from '../server.js';
3+
4+
const handleSignIn=(db, bcrypt, req, res)=>{
5+
const {email, password}=req.body;
6+
if(!email || !password){
7+
return Promise.reject('incorrect form submission');
8+
}
9+
return db.select('email', 'hash').from('login')
10+
.where('email', '=', email)
11+
.then(data=>{
12+
const isValid=bcrypt.compareSync(password, data[0].hash)
13+
if(isValid) {
14+
return db.select('*').from('users')
15+
.where('email', '=', email)
16+
.then(user=>user[0])
17+
.catch(err=>Promise.reject('unable to get user'))
18+
} else {
19+
Promise.reject('invalid credentials')
20+
}
21+
})
22+
.catch(err=>Promise.reject('invalid credentials'))
23+
}
24+
25+
export const getAuthTokenId=(req, res)=>{
26+
const {authorization}=req.headers;
27+
return redisClient.get(authorization, (err, reply)=>{
28+
if(err || !reply){
29+
return res.status(400).json('Unauthorised');
30+
}
31+
return res.json({id: reply})
32+
})
33+
}
34+
35+
const signToken=(email)=>{
36+
const jwtPayload = {email};
37+
return jwt.sign(jwtPayload, 'JWT_SECRET');
38+
}
39+
40+
const setToken=(key, value)=>{
41+
return Promise.resolve(redisClient.set(key, value))
42+
}
43+
44+
export const createSession=(user)=>{
45+
const {email, id} = user;
46+
const token= signToken(email);
47+
return setToken(token, id)
48+
.then (()=>{
49+
return {success: 'true', userId: id, token}
50+
})
51+
.catch(console.log)
52+
}
53+
54+
export const signinAuthentication=(db, bcrypt)=>(req, res)=>{
55+
const {authorization} =req.headers;
56+
return authorization? getAuthTokenId(req, res) :
57+
handleSignIn(db, bcrypt, req, res)
58+
.then (data=>{
59+
return data.id && data.email ? createSession(data) : Promise.reject(data)
60+
})
61+
.then (session=> res.json(session))
62+
.catch(err=>res.status(400).json(err))
63+
}

controllers/signout.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import redisClient from '../server.js';
2+
3+
const handleSignOut=((req, res)=>{
4+
const {authorization}=req.headers;
5+
redisClient.del(authorization);
6+
res.json('successfully logged out');
7+
})
8+
9+
export default handleSignOut;

docker-compose.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
services:
2+
3+
# Backend API
4+
smartbrainapi:
5+
container_name: backend
6+
build: ./
7+
command: npm start
8+
working_dir: /usr/src/smartbrainapi
9+
environment:
10+
POSTGRES_URI: postgres://admin:password@postgres:5432/smartbrain
11+
REDIS_URI: redis://redis:6379
12+
ports:
13+
- "3000:3000"
14+
volumes:
15+
- ./:/usr/src/smartbrainapi
16+
17+
# Postgres
18+
postgres:
19+
environment:
20+
POSTGRES_USER: admin
21+
POSTGRES_PASSWORD: password
22+
POSTGRES_DB: smartbrain
23+
POSTGRES_HOST: postgres
24+
build: ./postgres
25+
ports:
26+
- "5432:5432"
27+
28+
# Redis
29+
redis:
30+
image: redis
31+
ports:
32+
- "6379:6379"

0 commit comments

Comments
 (0)