Problem: Authenticate Users with Configurable Login and Tokens
Problem statement
You’re designing a new authentication system for your platform. It must support multiple login methods (password, OTP, Google) and multiple token types (session, JWT, encrypted).
Authentication systems often combine login and token generation logic in a single file, making them difficult to extend and maintain as new methods are introduced. A typical implementation might look like this:
if (method === 'password') verifyPassword();else if (method === 'otp') verifyOtp();else if (method === 'google') verifyGoogle();if (tokenType === 'jwt') issueJwt();else if (tokenType === 'session') issueSession();
The design doesn’t scale—every time you add or modify a login or token type, you have to change core logic.
You’ve decided to use the Strategy Pattern twice:
One family of strategies for authentication
Another for token generation
The base contexts (AuthService and TokenService) and all token strategies are already implemented. Your task is to implement the authentication strategies and a LoginController that orchestrates both. The controller should handle the complete workflow: authenticate → issue token.
Goal
Implement the missing pieces to complete the dual-strategy system.
Auth strategies:
PasswordAuth: Simulate verifying{ username, password }.OtpAuth: Simulate verifying{ phone, otp }.GoogleAuth: Simulate verifying{ token }.Each defines an async
.authenticate(credentials)method that returnstrueorfalse.
LoginController:Accepts both contexts (
AuthServiceandTokenService) via constructor.Implements
async login(credentials)that authenticates the user, then issues a token if authentication succeeds.
Constraints
Do not modify
AuthServiceorTokenService.In authentication strategies, simulate verification as follows:
PasswordAuth: Returntrueonly ifpassword === 'secret'.OtpAuth: Returntrueonly ifotp === 1234.GoogleAuth: Returntrueonly iftokenstarts with'valid-'.
Use
setTimeoutto simulate async verification in auth strategies.Use
Math.random().toString(36).slice(2, 8)for random IDs.Return clear console logs showing which strategies are active.
Sample output
The examples below illustrate what the output should look like:
(async () => {const authService = new AuthService(new PasswordAuth());const tokenService = new TokenService(new JwtToken());const controller = new LoginController(authService, tokenService);await controller.login({ username: 'alice', password: 'secret' });/* Expected output (may vary):[PasswordAuth] Authenticating alice...[JwtToken] Issued token: jwt.YWxpY2UuMTYzNQ.fakeSig*/authService.setStrategy(new OtpAuth());tokenService.setStrategy(new EncryptedToken());await controller.login({ phone: '+15551234', otp: 1234 });/* Expected output (may vary):[OtpAuth] Verifying +15551234 with code 1234...[EncryptedToken] Issued token: enc::eyJuYW1lIjoiYm9iIn0=*/authService.setStrategy(new GoogleAuth());tokenService.setStrategy(new SessionToken());await controller.login({ token: 'valid-xyz' });/* Expected output (may vary):[GoogleAuth] Checking Google token: valid-xyz...[SessionToken] Issued token: sess-4f2acb*/})();
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Authenticate Users with Configurable Login and Tokens
Problem statement
You’re designing a new authentication system for your platform. It must support multiple login methods (password, OTP, Google) and multiple token types (session, JWT, encrypted).
Authentication systems often combine login and token generation logic in a single file, making them difficult to extend and maintain as new methods are introduced. A typical implementation might look like this:
if (method === 'password') verifyPassword();else if (method === 'otp') verifyOtp();else if (method === 'google') verifyGoogle();if (tokenType === 'jwt') issueJwt();else if (tokenType === 'session') issueSession();
The design doesn’t scale—every time you add or modify a login or token type, you have to change core logic.
You’ve decided to use the Strategy Pattern twice:
One family of strategies for authentication
Another for token generation
The base contexts (AuthService and TokenService) and all token strategies are already implemented. Your task is to implement the authentication strategies and a LoginController that orchestrates both. The controller should handle the complete workflow: authenticate → issue token.
Goal
Implement the missing pieces to complete the dual-strategy system.
Auth strategies:
PasswordAuth: Simulate verifying{ username, password }.OtpAuth: Simulate verifying{ phone, otp }.GoogleAuth: Simulate verifying{ token }.Each defines an async
.authenticate(credentials)method that returnstrueorfalse.
LoginController:Accepts both contexts (
AuthServiceandTokenService) via constructor.Implements
async login(credentials)that authenticates the user, then issues a token if authentication succeeds.
Constraints
Do not modify
AuthServiceorTokenService.In authentication strategies, simulate verification as follows:
PasswordAuth: Returntrueonly ifpassword === 'secret'.OtpAuth: Returntrueonly ifotp === 1234.GoogleAuth: Returntrueonly iftokenstarts with'valid-'.
Use
setTimeoutto simulate async verification in auth strategies.Use
Math.random().toString(36).slice(2, 8)for random IDs.Return clear console logs showing which strategies are active.
Sample output
The examples below illustrate what the output should look like:
(async () => {const authService = new AuthService(new PasswordAuth());const tokenService = new TokenService(new JwtToken());const controller = new LoginController(authService, tokenService);await controller.login({ username: 'alice', password: 'secret' });/* Expected output (may vary):[PasswordAuth] Authenticating alice...[JwtToken] Issued token: jwt.YWxpY2UuMTYzNQ.fakeSig*/authService.setStrategy(new OtpAuth());tokenService.setStrategy(new EncryptedToken());await controller.login({ phone: '+15551234', otp: 1234 });/* Expected output (may vary):[OtpAuth] Verifying +15551234 with code 1234...[EncryptedToken] Issued token: enc::eyJuYW1lIjoiYm9iIn0=*/authService.setStrategy(new GoogleAuth());tokenService.setStrategy(new SessionToken());await controller.login({ token: 'valid-xyz' });/* Expected output (may vary):[GoogleAuth] Checking Google token: valid-xyz...[SessionToken] Issued token: sess-4f2acb*/})();
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.