124 lines
3.4 KiB
JavaScript
124 lines
3.4 KiB
JavaScript
import express from 'express';
|
|
import dotenv from 'dotenv';
|
|
|
|
import {RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate} from './database.js';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
|
|
app.use((err,req,res,next)=>{
|
|
console.error(err.stack);
|
|
res.status(500).send("Something went wrong!");
|
|
})
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
app.get("/register", async (req,res)=>{
|
|
const {email, password} = req.query;
|
|
|
|
if(!email || !password){
|
|
res.status(403).json({error: "Missing credentials"});
|
|
return;
|
|
}
|
|
|
|
const newUser = await RegisterNewUser(email,password);
|
|
if(!newUser){
|
|
res.status(403).json({error: "Email exists"});
|
|
}
|
|
|
|
res.status(201).json(newUser);
|
|
})
|
|
|
|
app.get("/login", async(req,res)=>{
|
|
const {email, pubkey} = req.query;
|
|
if(!email && !pubkey){
|
|
res.status(403).json({error:"Neither email nor pubkey was sent"})
|
|
return;
|
|
}
|
|
|
|
if(email){
|
|
const user = await GetUserFromEmail(email);
|
|
if(user){
|
|
res.status(200).json(user);
|
|
}else{
|
|
res.status(404).json({error:"User couldnt be found"})
|
|
}
|
|
}else if(pubkey){
|
|
const user = await GetUserFromPubkey(pubkey);
|
|
if(user){
|
|
res.status(200).json(user);
|
|
}else{
|
|
res.status(404).json({error:"User couldnt be found"})
|
|
}
|
|
}
|
|
})
|
|
|
|
app.get("/authenticate", async(req,res)=>{
|
|
const {email, password} = req.query;
|
|
if(!email || !password){
|
|
res.status(403).json({error:"Credentials missing"})
|
|
return;
|
|
}
|
|
|
|
const authResult = await Authneticate(email,password);
|
|
if(authResult){
|
|
res.status(200).json({authenticated: true})
|
|
}else{
|
|
res.status(403).json({authenticated: false})
|
|
}
|
|
})
|
|
|
|
app.get("/getKeypairWithPassword", async(req,res)=>{
|
|
const {email, password} = req.query;
|
|
if(!email || !password){
|
|
res.status(403).json({error:"Credentials missing"})
|
|
return;
|
|
}
|
|
|
|
const authResult = await Authneticate(email,password);
|
|
if(authResult){
|
|
const user = await GetKeypairFromEmail(email);
|
|
res.status(200).json(user)
|
|
}else{
|
|
res.status(403).json({authenticated: false})
|
|
}
|
|
})
|
|
|
|
app.get("/getPubkey", async(req,res)=>{
|
|
const {email}= req.query;
|
|
|
|
if(!email){
|
|
res.status(403).json({error: "Missing credentials"});
|
|
return;
|
|
}
|
|
|
|
const user = await GetUserFromEmail(email);
|
|
res.status(200).json(user);
|
|
})
|
|
|
|
app.get("/getKeypair", async(req,res)=>{
|
|
const {email, apiKey}= req.query;
|
|
|
|
if(!email || !apiKey){
|
|
res.status(403).json({error: "Missing credentials"});
|
|
return;
|
|
}
|
|
if(apiKey!= process.env.API_KEY){
|
|
res.status(403).json({error: `Invalid Api Key`});
|
|
console.log(`Provided api key: ${apiKey}`);
|
|
console.log(`Correcte api key: ${process.env.API_KEY}`);
|
|
return;
|
|
}
|
|
|
|
const user = await GetKeypairFromEmail(email);
|
|
res.status(200).json(user);
|
|
})
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
app.listen(process.env.EXPRESS_PORT, ()=>{
|
|
console.log(`Server is listening on Port ${process.env.EXPRESS_PORT}`);
|
|
}) |