stable
This commit is contained in:
115
index.js
115
index.js
@@ -1,21 +1,21 @@
|
||||
import express from 'express';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
import {RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate} from './database.js';
|
||||
import { RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate, CreateNewRequest, SetRequestResult } from './database.js';
|
||||
import { verifyFirebaseToken } from './firebase.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
|
||||
import cors from 'cors';
|
||||
app.use(cors());
|
||||
app.use((err,req,res,next)=>{
|
||||
console.error(err.stack);
|
||||
res.status(500).send("Something went wrong!");
|
||||
})
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
app.get("/register", async (req,res)=>{
|
||||
app.get("/registerv1", async (req,res)=>{
|
||||
const {email, password} = req.query;
|
||||
|
||||
if(!email || !password){
|
||||
@@ -31,13 +31,43 @@ app.get("/register", async (req,res)=>{
|
||||
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"})
|
||||
app.get("/register", async (req,res)=>{
|
||||
const {tokenId} = req.query;
|
||||
|
||||
if(!tokenId){
|
||||
res.status(403).json({error: "Missing tokenId"});
|
||||
return;
|
||||
}
|
||||
const user = await verifyFirebaseToken(tokenId);
|
||||
if(!user){
|
||||
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const newUser = await RegisterNewUser(user.uid,user.uid);
|
||||
|
||||
if(!newUser){
|
||||
res.status(403).json({error: "User exists"});
|
||||
}
|
||||
|
||||
res.status(201).json(newUser);
|
||||
})
|
||||
|
||||
app.get("/login", async(req,res)=>{
|
||||
const {tokenId} = req.query;
|
||||
if(!tokenId){
|
||||
res.status(403).json({error:"Missing tokenId"})
|
||||
return;
|
||||
}
|
||||
|
||||
const fuser = await verifyFirebaseToken(tokenId);
|
||||
if(!fuser){
|
||||
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||
return;
|
||||
}
|
||||
const email = fuser.uid;
|
||||
|
||||
if(email){
|
||||
const user = await GetUserFromEmail(email);
|
||||
if(user){
|
||||
@@ -45,13 +75,6 @@ app.get("/login", async(req,res)=>{
|
||||
}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"})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -70,20 +93,21 @@ app.get("/authenticate", async(req,res)=>{
|
||||
}
|
||||
})
|
||||
|
||||
app.get("/getKeypairWithPassword", async(req,res)=>{
|
||||
const {email, password} = req.query;
|
||||
if(!email || !password){
|
||||
res.status(403).json({error:"Credentials missing"})
|
||||
app.get("/getKeypairWithToken", async(req,res)=>{
|
||||
const {tokenId} = req.query;
|
||||
if(!tokenId){
|
||||
res.status(403).json({error:"tokenId 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})
|
||||
const fuser = await verifyFirebaseToken(tokenId);
|
||||
if(!fuser){
|
||||
res.status(403).json({error:"Invalid tokenId. Auth failed"});
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await GetKeypairFromEmail(fuser.uid);
|
||||
res.status(200).json(user);
|
||||
})
|
||||
|
||||
app.get("/getPubkey", async(req,res)=>{
|
||||
@@ -116,6 +140,45 @@ app.get("/getKeypair", async(req,res)=>{
|
||||
res.status(200).json(user);
|
||||
})
|
||||
|
||||
app.get("/firebaseAuth", async (req,res)=>{
|
||||
const {tokenId} = req.query;
|
||||
|
||||
if(!tokenId){
|
||||
res.status(403).json({error: "Invalid tokenID"});
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await verifyFirebaseToken(tokenId);
|
||||
if(user){
|
||||
res.status(200).json({status:"Success", user: user});
|
||||
}else{
|
||||
res.status(403).json({status:"Failed", user:null});
|
||||
}
|
||||
})
|
||||
|
||||
app.get("/createNewRequest", async(req,res)=>{
|
||||
const id = await CreateNewRequest();
|
||||
|
||||
res.status(200).json({id:id});
|
||||
})
|
||||
|
||||
app.get("/completeRequest", async(req,res)=>{
|
||||
const {id,result}=req.query;
|
||||
|
||||
if(!id || !result){
|
||||
res.status(403).json({error:"Missing parameters, need id and result"});
|
||||
return;
|
||||
}
|
||||
|
||||
await SetRequestResult(id,result);
|
||||
|
||||
res.status(200).json({status:"Success"});
|
||||
})
|
||||
|
||||
app.get("/status", async(req,res)=>{
|
||||
res.status(200).json({status:"Running"});
|
||||
})
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user