Skip to content

Commit

Permalink
Merge pull request #249 from Xaxxoo/redirect
Browse files Browse the repository at this point in the history
landing page redirect
  • Loading branch information
Solomonsolomonsolomon authored Dec 31, 2024
2 parents eb33b90 + fbfad39 commit 33f9695
Show file tree
Hide file tree
Showing 15 changed files with 1,333 additions and 6,943 deletions.
843 changes: 625 additions & 218 deletions land-registry-backend/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion land-registry-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"dotenv": "^16.4.7",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"helmet": "^7.0.0",
Expand Down
10 changes: 10 additions & 0 deletions land-registry-backend/src/@types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Request } from "express";
import { IUser } from "../models/user.model";

declare global {
namespace Express {
interface Request {
user?: IUser;
}
}
}
3 changes: 2 additions & 1 deletion land-registry-backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ dotenv.config();
export const config = {
port: process.env.PORT || 3000,
databaseUrl: process.env.DATABASE_URL || '',
ssl: false,
environment: process.env.NODE_ENV || 'development',
corsOrigins: process.env.CORS_ORIGINS?.split(',') || ['http://localhost:3000'],
corsOrigins: process.env.CORS_ORIGINS?.split(',') || ['http://localhost:3001'],
jwtSecret: process.env.JWT_SECRET || 'your-default-secret-key',
};
33 changes: 30 additions & 3 deletions land-registry-backend/src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as authService from '../services/authService';
export async function register(req: Request, res: Response, next: NextFunction) {
try {
const user = await authService.register(req.body);
res.status(201).json(user);
res.status(201).json({
status: "success",
message: "User signed in successfully",
data: user
});
} catch (error) {
next(error);
}
Expand All @@ -13,8 +17,31 @@ export async function register(req: Request, res: Response, next: NextFunction)
export async function login(req: Request, res: Response, next: NextFunction) {
try {
const result = await authService.login(req.body);
res.json(result);
res.status(201).json({
status: "success",
message: "User logged successfully",
data: result
});
} catch (error) {
next(error);
}
}
}


//Logout user
export const logoutUser =
async (req: Request, res: Response, next: NextFunction) => {
try {
res.cookie("landver_token", "", { maxAge: 1 });
// res.cookie("refresh_token", "", { maxAge: 1 });

const userId = req.user?._id || "";

res.status(200).json({
success: true,
message: "Logged Out successfully",
});
} catch (error: any) {
return next(error);
}
}
35 changes: 32 additions & 3 deletions land-registry-backend/src/services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { query } from "./db";
import { RegisterDTO, LoginDTO, User } from "../types/auth";
import { AppError } from "../middleware/errorHandler";
import { config } from "../config";
import dotenv from "dotenv";
dotenv.config();

export async function register(data: RegisterDTO): Promise<User> {
export async function register(
data: RegisterDTO
): Promise<{ token: string; user: User }> {
// Check if user already exists
const existingUser = await query(
"SELECT * FROM users WHERE email = $1 OR wallet_address = $2",
Expand All @@ -30,7 +34,32 @@ export async function register(data: RegisterDTO): Promise<User> {
[data.email, passwordHash, data.walletAddress, data.userType]
);

return result.rows[0];
const user: User = result.rows[0];

const token = jwt.sign(
{
email: user.email,
walletAddress: user.walletAddress,
userType: user.userType,
},
config.jwtSecret,
{ expiresIn: "1h" } // Token expires in 1 hour
);

console.log(token);

// Return the user and token
return {
user: {
id: user.id,
email: user.email,
walletAddress: user.walletAddress,
userType: user.userType,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
},
token,
};
}

export async function login(
Expand Down Expand Up @@ -58,7 +87,7 @@ export async function login(
config.jwtSecret,
{ expiresIn: "24h" }
);

return {
token,
user: {
Expand Down
1 change: 1 addition & 0 deletions land-registry-backend/src/services/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Pool } from 'pg';
import { config } from '../config';

export const pool = new Pool({

connectionString: config.databaseUrl,
ssl: {
rejectUnauthorized: false
Expand Down
Loading

0 comments on commit 33f9695

Please sign in to comment.