Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loginUserWithWallet() #38

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@aws-sdk/client-ses": "^3.658.1",
"@polkadot/util-crypto": "^13.1.1",
"@typegoose/typegoose": "^12.3.1",
"axios": "^1.7.4",
"cors": "^2.8.5",
Expand Down
41 changes: 34 additions & 7 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { UserModel } from "@/models/User";
import { sendVerificationEmail } from "@/helpers/aws/ses";
import { signatureVerify } from "@polkadot/util-crypto";

export const createUser = async (req: Request, res: Response) => {
try {
Expand All @@ -14,7 +15,7 @@ export const createUser = async (req: Request, res: Response) => {
return res.status(200).send(newUser);
}
} catch (e) {
console.error(`[ERROR][createUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][createUser] ${e}`);
}

return res.status(400).send({
Expand All @@ -32,7 +33,7 @@ export const getUser = async (req: Request, res: Response) => {
const user = await UserModel.findOne({ _id: id }, { email: 1, name: 1, company: 1, isAdmin: 1, picture: 1 });
if (user) return res.status(200).send(user);
} catch (e) {
console.error(`[ERROR][getUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][getUser] ${e}`);
}

return res.status(400).send({
Expand Down Expand Up @@ -64,7 +65,7 @@ export const updateUser = async (req: Request, res: Response) => {
isAdmin: user.isAdmin,
});
} catch (e) {
console.error(`[ERROR][updateUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][updateUser] ${e}`);
}

return res.status(400).send({
Expand All @@ -82,7 +83,7 @@ export const deleteUser = async (req: Request, res: Response) => {
const result = await UserModel.deleteOne({ _id: id });
if (result?.deletedCount > 0) return res.status(200).send({ message: `User '${id}' deleted` });
} catch (e) {
console.error(`[ERROR][deleteUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][deleteUser] ${e}`);
}

return res.status(400).send({
Expand All @@ -100,7 +101,7 @@ export const loginUser = async (req: Request, res: Response) => {
const authToken = await UserModel.login(email, password, true);
if (authToken) return res.status(200).send({ jwt: authToken });
} catch (e) {
console.error(`[ERROR][loginUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][loginUser] ${e}`);
}

return res.status(400).send({
Expand All @@ -117,12 +118,38 @@ export const loginUserWithGoogle = async (req: Request, res: Response) => {
const user = await UserModel.findOne({ email });
if (user) return res.status(200).send({ jwt: user.getAuthToken(true) });
else {
await UserModel.createUser(email, `google#${email}`, name, " ", picture);
await UserModel.createUser(email, "", name, "Google", picture);
const user = await UserModel.findOne({ email });
if (user) return res.status(200).send({ jwt: user.getAuthToken(true) });
}
} catch (e) {
console.error(`[ERROR][loginUser] ${JSON.stringify(e)}`);
console.error(`[ERROR][loginUserWithGoogle] ${e}`);
}
return res.status(400).send({
error: {
message: "Invalid login",
},
});
};

export const loginUserWithWallet = async (req: Request, res: Response) => {
try {
const { address, name, signature } = req.body;
if (!address || !signature) return res.status(400).send({ error: { message: "Missing address or signature" } });

const { isValid } = signatureVerify(`${address}@PolkadotEducation`, signature, address);
if (!isValid) return res.status(400).send({ error: { message: "Invalid Signature" } });

const addr = String(address).toLowerCase();
const user = await UserModel.findOne({ email: addr });
if (user) return res.status(200).send({ jwt: user.getAuthToken(true) });
else {
await UserModel.createUser(addr, "", name || `${address.slice(0, 5)}...${address.slice(-5)}`, "Web3", "");
const user = await UserModel.findOne({ email: addr });
if (user) return res.status(200).send({ jwt: user.getAuthToken(true) });
}
} catch (e) {
console.error(`[ERROR][loginUserWithWallet] ${e}`);
}
return res.status(400).send({
error: {
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class User extends BaseModel {
}
const user = await UserModel.create({
email: email.toLowerCase(),
password: await this.hashPassword(password),
password: await this.hashPassword(password || crypto.randomBytes(16).toString("hex")),
name,
company,
picture,
Expand Down
11 changes: 10 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Express, Request, Response } from "express";

// Controllers
import { createUser, deleteUser, getUser, loginUser, loginUserWithGoogle, updateUser } from "@/controllers/users";
import {
createUser,
deleteUser,
getUser,
loginUser,
loginUserWithGoogle,
loginUserWithWallet,
updateUser,
} from "@/controllers/users";
import { createLesson, deleteLesson, getLesson, updateLesson } from "@/controllers/lessons";
import { createModule, deleteModule, getModule, updateModule } from "@/controllers/modules";
import { createCourse, deleteCourse, getCourse, updateCourse, duplicateCourse } from "@/controllers/courses";
Expand All @@ -19,6 +27,7 @@ const router = (app: Express) => {
app.delete("/users/:id", [authMiddleware], deleteUser);
app.post("/users/login", loginUser);
app.post("/users/login/google", loginUserWithGoogle);
app.post("/users/login/wallet", loginUserWithWallet);

// Lessons
app.post("/lesson", [authMiddleware], createLesson);
Expand Down
Loading