-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(core) Config to disable Anonymous session creation #2990
feat(core) Config to disable Anonymous session creation #2990
Conversation
…ted users to add to cart. This helps preventing bot/scrapers to make operations on shop-api, allows only authenticated users to view products etc.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Hi, Thanks for the suggestion. Before going with adding another config value, I'd like to explore how this can be done using middleware and thus not requiring a new config property (I bias towards only adding new config props when absolutely necessary). Here's a suggestion you can try. I didn't test it yet but I think this or something very similar should work: // src/plugins/disable-anonymous-access/api/middleware.ts
import { Injectable, NestMiddleware } from '@nestjs/common'
import {
CachedSession,
ConfigService,
SessionService,
extractSessionToken,
} from '@vendure/core'
import { NextFunction, Request, Response } from 'express'
@Injectable()
export class DisableAnonymousAccessMiddleware implements NestMiddleware {
constructor(
private sessionService: SessionService,
private configService: ConfigService,
) {}
async use(req: Request, res: Response, next: NextFunction) {
const isLoggedIn = await this.isLoggedIn(req)
if (!isLoggedIn) {
return res.status(403).send('Forbidden')
}
next()
}
private async isLoggedIn(req: Request): Promise<boolean> {
const session = await this.getSession(req)
return !!session?.user
}
private async getSession(req: Request): Promise<CachedSession | undefined> {
const { tokenMethod } = this.configService.authOptions
const sessionToken = extractSessionToken(req, tokenMethod)
return sessionToken
? await this.sessionService.getSessionFromToken(sessionToken)
: undefined
}
} // src/plugins/disable-anonymous-access/disable-anonymous-access.plugin.ts
import { MiddlewareConsumer, NestModule } from '@nestjs/common'
import { PluginCommonModule, VendurePlugin } from '@vendure/core'
import { DisableAnonymousAccessMiddleware } from './api/middleware'
@VendurePlugin({
// ... omitted
})
export class DisableAnonymousAccessPlugin implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(DisableAnonymousAccessMiddleware).forRoutes('shop-api')
}
} |
I agree with that. I am launching UserCheckout plugin today which I must have this feature conjuction with that. Let me see if I can make it with a plugin. other than that I did not see any side effects of this. I spent last 3 days on Vendure Auth and User checkout learnt a lot. |
Main hickup I had with the system that Permission.Authenticated does not aspply to users system wide. It is tied to roles Customer and Admins. Any external auth shoul implement its role and Push Autheticated to that role which I will do that on firebase. User entity is not much of a thing without being a customer. |
There is a catch 22, it needs to access to authenticate method and middleware blocks all. I figured it out by parsing body and allowing methods as public |
Great thanks for sharing your solution as a plugin too! |
Description
Added config to disable Anonymous session creation. This allows only authenticated users to add to cart. It helps preventing bot/scrapers to make operations on shop-api, allows only authenticated users to view products etc.
Default is false
Breaking changes
No
Checklist
📌 Always:
👍 Most of the time:
I have read the CLA Document and I hereby sign the CLA