I want to get detailed data for the user who is currently logged in, among these data are data from the user table and data from the profile table, the problem is when I use the access token to find out which user is currently logged in, I cannot get the profile data related to the user table
This is Entity for User Table
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
userName: string;
@Column({ unique: true, nullable: true })
email: string;
@Column({ unique: true })
phoneNumber: string;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
@OneToOne(() => Profile, (profile) => profile.user)
profile: Profile;
refreshTokens: UserRefreshToken[];`
And This is Entity for Profile Table
@Entity()
export class Profile extends BaseEntity {
@PrimaryGeneratedColumn('increment')
id: number;
@Column({ nullable: true })
name: string;
@Column({ nullable: true })
gender: string;
@Column({ nullable: true })
phoneNumber: string;
@Column({ nullable: true })
email: string;
@Column({ nullable: true })
nrm: string;
@Column({ nullable: true })
birthDate: Date;
@CreateDateColumn({ nullable: true })
createdAt: Date;
@UpdateDateColumn({ nullable: true })
updatedAt: Date;
@OneToOne(() => Users)
@JoinColumn()
user: Users;
}
This is example function to get the currently logged in user
import { Controller, Get, UseGuards } from '@nestjs/common';
import { ProfileService } from './profile.service';
import { accessTokenGuard } from 'src/guards/access-token.guard';
import { GetUser } from 'src/modules/auth/get-user-decorator';
import { Users } from '../users/entity/users.entity';
@Controller('profile')
export class ProfileController {
constructor(private service: ProfileService) {}
@UseGuards(accessTokenGuard)
@Get()
async getData(@GetUser() user: Users) {
return user; // example get the currently logged in user
}
}
This is Custom decorator to call currntly logged user
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
export const GetUser = createParamDecorator(
(_data, ctx: ExecutionContext): any => {
const req = ctx.switchToHttp().getRequest();
console.log(req.user);
return req.user;
},
);
in controller i get response like this
{
"id": 41,
"userName": "6282340947878",
"email": null,
"phoneNumber": "6282340947878",
"createdAt": "2023-06-25T07:35:57.467Z",
"updatedAt": "2023-06-25T07:35:57.461Z"
}
but I want to get a response like this, without having to query the database again using only the access token
{
"id": 41,
"userName": "6282340947878",
"email": null,
"phoneNumber": "6282340947878",
"createdAt": "2023-06-25T07:35:57.467Z",
"updatedAt": "2023-06-25T07:35:57.461Z",
"profile": {
"id": 36,
"name": "Profile Name",
"gender": "Male",
"phoneNumber": "6282340947878",
"email": "",
"nrm": "19.02.05",
"birthDate": "2000-01-07T08:00:00.000Z",
"createdAt": "2023-06-25T07:36:32.586Z",
"updatedAt": "2023-06-25T07:36:32.586Z"
}
}
Robby,
Seems to me like you are missing a step. The decorator you have made is returning the user object that has been attached to the request. This attachment is made somewhere - likely in a access token strategy that accompanies your access token guard.
In the strategy, you are likely to find a validate
method, and this method is likely pulling the user data out of the database. In that very place, you should be joining in the profile data on user.id = profile.user_id or something similar.
If you would show us your validate method, or how you are otherwise populating this user object that is attached to the request we could help you further...
Good luck