Skip to content

Commit

Permalink
Merge pull request #12 from PolkadotEducation/artur-login-google
Browse files Browse the repository at this point in the history
Add loginWithGoogle + user.picture
  • Loading branch information
arturgontijo authored Sep 2, 2024
2 parents 799d60a + cfc976f commit 83071b9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV=local
MONGODB_URI=mongodb://0.0.0.0:27017/doteducation
MONGODB_URI=mongodb://0.0.0.0:27017/doteducation-local
SERVER_HOST=0.0.0.0
SERVER_PORT=4000
JWT_SECRET=OR5t0v7yk3z7qw77
Expand Down
21 changes: 21 additions & 0 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,24 @@ export const loginUser = async (req: Request, res: Response) => {
},
});
};

export const loginUserWithGoogle = async (req: Request, res: Response) => {
try {
const { email, name, picture } = req.body;
if (!email) return res.status(400).send({ error: { message: "Missing email" } });
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);
const user = await UserModel.findOne({ email });
if (user) return res.status(200).send({ jwt: user.getAuthToken(true) });
}
} catch (e) {
console.log(`[ERROR][loginUser] ${JSON.stringify(e)}`);
}
return res.status(400).send({
error: {
message: "Invalid login",
},
});
};
11 changes: 9 additions & 2 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class User extends BaseModel {
@prop({ required: true, type: String })
public name: string;

@prop({ required: true, type: String, default: "" })
@prop({ required: false, type: String })
public picture: string;

@prop({ required: true, type: String, default: " " })
public company: string;

@prop({ required: true, type: Boolean, default: false })
Expand Down Expand Up @@ -46,6 +49,7 @@ class User extends BaseModel {
password: string,
name: string,
company: string,
picture?: string,
isAdmin?: boolean,
): Promise<UserInfo | undefined> {
try {
Expand All @@ -57,7 +61,8 @@ class User extends BaseModel {
email: email.toLowerCase(),
password: await this.hashPassword(password),
name,
company: company || "",
company,
picture,
isAdmin: isAdmin || false,
verifyToken: randomBytes(16).toString("hex"),
lastActivity: new Date(),
Expand All @@ -67,6 +72,7 @@ class User extends BaseModel {
email: user.email,
name: user.name,
company: user.company,
picture: user.picture,
isAdmin: user.isAdmin,
verifyToken: user.verifyToken,
lastActivity: user.lastActivity,
Expand Down Expand Up @@ -96,6 +102,7 @@ class User extends BaseModel {
email: this.email,
name: this.name,
company: this.company,
picture: this.picture,
isAdmin: this.isAdmin,
},
createdAt: moment().unix(),
Expand Down
3 changes: 2 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Express, Request, Response } from "express";

// Controllers
import { createUser, deleteUser, getUser, loginUser, updateUser } from "@/controllers/users";
import { createUser, deleteUser, getUser, loginUser, loginUserWithGoogle, updateUser } from "@/controllers/users";

import { createLesson, deleteLesson, getLesson, updateLesson } from "@/controllers/lessons";
import authMiddleware from "./middlewares/auth";
Expand All @@ -17,6 +17,7 @@ const router = (app: Express) => {
app.put("/users/:id", [corsConfig(), authMiddleware], updateUser);
app.delete("/users/:id", [corsConfig(), authMiddleware], deleteUser);
app.post("/users/login", loginUser);
app.post("/users/login/google", loginUserWithGoogle);

// Tracks
// Modules
Expand Down
1 change: 1 addition & 0 deletions src/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type UserInfo = {
email: string;
name: string;
company: string;
picture: string;
isAdmin: boolean;
lastActivity: Date;
verifyToken?: string;
Expand Down
6 changes: 3 additions & 3 deletions tests/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Setting API Server up...", () => {
const email = "user2@polkadot.education";
const name = "User Two";
const password = "superSecret";
const user = await UserModel.createUser(email, password, name, "company", false);
const user = await UserModel.createUser(email, password, name, "company", "Base64OrLink", false);
await axios
.get(`${API_URL}/users/${user?.userId}`)
.then((r) => {
Expand All @@ -68,7 +68,7 @@ describe("Setting API Server up...", () => {
const email = "user3@polkadot.education";
const name = "User Three";
const password = "superSecret";
const user = await UserModel.createUser(email, password, name, "company", false);
const user = await UserModel.createUser(email, password, name, "company", "Base64OrLink", false);

const newEmail = "New Email";
const newPassword = "newSuperSecret";
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("Setting API Server up...", () => {
const email = "user4@polkadot.education";
const name = "User Four";
const password = "superSecret";
const user = await UserModel.createUser(email, password, name, "company", false);
const user = await UserModel.createUser(email, password, name, "company", "Base64OrLink", false);
await axios
.delete(`${API_URL}/users/${user?.userId}`)
.then((r) => {
Expand Down

0 comments on commit 83071b9

Please sign in to comment.