prodv1
This commit is contained in:
127
index.js
127
index.js
@@ -1,7 +1,124 @@
|
||||
import {Keypair} from '@solana/web3.js';
|
||||
import bs58 from 'bs58';
|
||||
import express from 'express';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
const newWallet = Keypair.generate();
|
||||
import {RegisterNewUser, GetUserFromEmail, GetUserFromPubkey, GetKeypairFromEmail, Authneticate} from './database.js';
|
||||
|
||||
console.log(newWallet.publicKey.toBase58());
|
||||
console.log(bs58.encode(newWallet.secretKey));
|
||||
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}`);
|
||||
})
|
||||
Reference in New Issue
Block a user