ગાર્ડ્સ નક્કી કરે છે કે શું વિનંતી રુટ હેન્ડલર તરફ આગળ વધવા માટે અનુમતિ છે — તેઓ NestJS ની સત્તાધિકરણ માટે સમર્પિત મેકેનિઝમ છે (અને ઘણીવાર પ્રમાણીકરણ ચેક). ગાર્ડ વિનંતીને મંજૂરી આપવા માટે true અથવા તેને બ્લોક કરવા માટે false/અપવાદ નાખે છે.
@Injectable()
export class AuthGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
return !!request.headers.authorization; // true = allow, false = block (403)
}
}
ગાર્ડ canActivate() લાગુ કરે છે, જે બુલિયન (અથવા તેનું Promise/Observable) આપે છે. false પાછું કરવાથી વિનંતી 403 સાથે બ્લોક થાય છે; તમે ચોક્કસ અપવાદ પણ નાખી શકો છો (દા.ત. UnauthorizedException).
@UseGuards(AuthGuard) // on a single route
@Get("profile")
getProfile() {}
@UseGuards(AuthGuard) // on a whole controller (all routes)
@Controller("admin")
export class AdminController {}
app.useGlobalGuards(new AuthGuard()); // globally (all routes)
ગાર્ડ્સ પદ્ધતિ, નિયંત્રક અથવા વૈશ્વિક સ્તરે લાગુ કરી શકાય છે.
@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private jwtService: JwtService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const token = request.headers.authorization?.split(" ")[1];
if (!token) throw new UnauthorizedException();
try {
request.user = await this.jwtService.verifyAsync(token); // attach the user
return true;
} catch {
throw new UnauthorizedException("Invalid token");
}
}
}
// a @Roles decorator stores required roles as metadata
@Roles("admin")
@Get("users")
listUsers() {}
// the RolesGuard reads the metadata and checks the user's roles
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(ctx: ExecutionContext): boolean {
const roles = this.reflector.get<string[]>("roles", ctx.getHandler());
const { user } = ctx.switchToHttp().getRequest();
return roles.some(role => user.roles?.includes(role)); // allow if the user has a required role
}
}
@Roles() શોધક (મેટાડેટા) ને ગાર્ડ સાથે જોડવું જે તેને વાંચે છે (હોય Reflector દ્વારા) એ ભૂમિકા-આધારિત ઍક્સેસ નિયંત્રણ માટે માનક પેટર્ન છે.
The ExecutionContext gives guards rich info (the handler, class, request) — more
than plain middleware — which is why guards (not middleware) are the right place
for authorization decisions.
ગાર્ડ્સ NestJS ની સમર્પિત, મુહાવરેદાર મેકેનિઝમ છે સત્તાધિકરણ માટે — પ્રમાણીકરણ અને અનુમતિઓના આધારે કઈ વિનંતીઓ હેન્ડલર સુધી પહોંચવાની મંજૂરી છે તે નિયંત્રણ કરવું.
તેમને સમજવું આવશ્યક છે કારણ કે લગભગ દરેક વાસ્તવિક એપ્લીકેશનને રુટ્સ રક્ષણ કરવો જોઈએ (લૉગિન જરૂરી, ભૂમિકાઓ/અનુમતિઓ તપાસો), અને ગાર્ડ્સ આ માટે સાચો સાધન છે (મિડલવેર અથવા દરેક હેન્ડલરમાં auth તર્કને છુપાવવાથી વધુ સ્વચ્છ).
માનક પેટર્ન — એક JWT/auth ગાર્ડ જે ક્રેડેન્શિયલ ચકાસે છે અને વિનંતીમાં વપરાશકર્તાને જોડે છે, અને ભૂમિકા-આધારિત ગાર્ડ્સ @Roles() શોધક સાથે જોડીને જે RBAC માટે Reflector-વાંચતું ગાર્ડ સાથે — NestJS API સુરક્ષાને મૂળભૂત છે.
ગાર્ડ્સની સમૃદ્ધ ExecutionContext ની ઍક્સેસ (લક્ષ્ય હેન્ડલર, વર્ગ અને વિનંતી જાણવું) ચોક્કસ કારણ છે કે તેઓ સત્તાધિકરણ નિર્ણયોને અનુકૂળ છે જે મિડલવેર સ્વચ્છતાથી કરી શકતું નથી.
જાણવું કે કેવી રીતે ગાર્ડ્સ લખવાં, લાગુ કરવાં (પદ્ધતિ/નિયંત્રક/વૈશ્વિક), અને મેટાડેટા શોધક સાથે જોડવાં તે NestJS અરજીઓને સુરક્ષિત બનાવવાની મુખ્ય જ્ઞાન છે અને ફ્રેમવર્કમાં વારંવાર, આર્કિટેક્ચરલી-મહત્વપૂર્ણ વિષય છે.