Hi @markbloomfield, thanks for your feedback. The reason I didn't include this part in the original tutorial series was because I thought it would complicate things for a reader. But if you are interested in an authenticated flow, I can extend the tutorial series with a part 4.
For now, Yes, you have to implement AuthService separately and use it within the AuthenticatedSocketIoAdapter class. To use it, you can use the variable this.authService. In fact, the const user = await this.authService.authenticateToken(token); line actually tries to authenticate the user using the token sent from frontend, and return the user object for that authenticated user.
For example, if the AuthService uses Firebase as the Auth provider, the AuthService class would look something like,
@Injectable()exportclassAuthService{constructor(privatereadonlyuserService:UserService,privatereadonlyfirebaseService:FirebaseService,){}asyncauthenticateWithFirebase(idToken:string){try{constdecodeUser=awaitthis.firebaseService.auth.verifyIdToken(idToken);if(!decodeUser){thrownewUnauthorizedException();}const{sub,groups}=decodeUser;constuser=awaitthis.userService.getUserByFirebaseUid(sub);// Get user from db.returnuser;}catch(e){thrownewSocialAuthenticationException();}}}
In the above code, the authenticateWithFirebase method call is used within the AuthenticatedSocketIoAdapter class.
Here the injected,
FirebaseService class contains just the firebase initialisation code.
UserService class contains the database-related methods (CRUD methods mainly) for user data.
Please tell me if this is not clear and please also tell me if I should extend the series with a part 4 explaining the authentication.
Very kind of you to reply, thanks @rukshanjs 🙏 For my current needs, I don't actually need authentication at the socket level, so for now I just immediately return next(); and later when necessary, I'll send a token from my React FE (NextAuth) to validate the timers.
My implementation is a little more complex than yours, but your tutorial has been invaluable, thank you.
Another question: Have you experimented with a way to show all active timers in a room?
Hi @iaremarkus I'm super glad my series has been helpful. Currently with my implementation, a single room is for a single user (one user can have several devices and all those devices are within that room). The identifiers for each timer contains the userId in it. So to get all the timers within a room, I think we can use the userId to find the timers that contain it in the identifier/key of each timer.
In this series I'm tracking the timers (for each user device) as a single variable. But I have tried with implementing a Redis connection to keep this state on there so even if the server crashes the timers are still saved.
Hi @markbloomfield, thanks for your feedback. The reason I didn't include this part in the original tutorial series was because I thought it would complicate things for a reader. But if you are interested in an authenticated flow, I can extend the tutorial series with a part 4.
For now, Yes, you have to implement
AuthServiceseparately and use it within theAuthenticatedSocketIoAdapterclass. To use it, you can use the variablethis.authService. In fact, theconst user = await this.authService.authenticateToken(token);line actually tries to authenticate the user using the token sent from frontend, and return the user object for that authenticated user.For example, if the
AuthServiceuses Firebase as the Auth provider, theAuthServiceclass would look something like,In the above code, the
authenticateWithFirebasemethod call is used within theAuthenticatedSocketIoAdapterclass.Here the injected,
FirebaseServiceclass contains just the firebase initialisation code.UserServiceclass contains the database-related methods (CRUD methods mainly) for user data.Please tell me if this is not clear and please also tell me if I should extend the series with a part 4 explaining the authentication.
Very kind of you to reply, thanks @rukshanjs 🙏 For my current needs, I don't actually need authentication at the socket level, so for now I just immediately
return next();and later when necessary, I'll send a token from my React FE (NextAuth) to validate the timers.My implementation is a little more complex than yours, but your tutorial has been invaluable, thank you.
Another question: Have you experimented with a way to show all active timers in a room?
Hi @iaremarkus I'm super glad my series has been helpful. Currently with my implementation, a single room is for a single user (one user can have several devices and all those devices are within that room). The identifiers for each timer contains the
userIdin it. So to get all the timers within a room, I think we can use theuserIdto find the timers that contain it in the identifier/key of each timer.In this series I'm tracking the timers (for each user device) as a single variable. But I have tried with implementing a Redis connection to keep this state on there so even if the server crashes the timers are still saved.
good tip, i'll give that a go.
thanks again for the great series 👌