name: skill_user_login description: A skill to implement user authentication (login/signin) with optional 2FA support. Based on IoT Tower Control backend. license: GPL-3.0 metadata: author: "disk91" version: "1.0.0"
User Login & Authentication using the IoT Tower Control backend
Overview
This skill allows you to implement user authentication (login/signin) with support for two-factor authentication (2FA). It is based on the IoT Tower Control backend, which provides a robust and scalable platform for building IoT platforms. This skill details how to create the front-end part of the authentication solution and how to integrate it with the backend. The login process is a multi-step process that handles various scenarios including password expiration, user condition validation, and 2FA authentication.
When to use this skill
When you've been asked to integrate user authentication, login, or signin functionality in the application.
How to use this skill
This skill helps to generate working code for creating a user authentication page. It contains examples in TypeScript, made for Nuxt.js 4 but the code can be adapted to other frameworks.
Principles of the login process
The login process is a public endpoint where a user can authenticate using:
Email: The user's email address used as login identifier.- The email should be validated to ensure it is in the correct format.
- The email is mandatory
Password: The user's password for authentication.- The password is mandatory
- The password is never stored in plain text on the backend
The authentication process is progressive and handles multiple scenarios:
- Basic Authentication (1FA): Initial login with email and password
- Password Expiration: User may need to change expired password before full access
- User Conditions: User may need to accept updated terms and conditions
- Two-Factor Authentication (2FA): Additional security layer if enabled
- Session Upgrade: Complete authentication process to gain full access
Workflow of the login process
The file user_login.md contains the workflow of the login process, with the different steps to implement in the front-end and the expected response from the backend. It also contains the structure and API calls to be performed with the list of i18n keys to use for displaying messages to the user.
As a functional summary:
Step 1: Initial Authentication (Sign In)
- The user fills the login form with email and password
- The form syntax is validated on the front-end
- It is submitted to the backend API endpoint with a
POSTrequest to/users/1.0/session/signin - The expected response is
200and it contains a JWT token with authentication status indicators:jwtToken: The authentication token for API callsjwtRenewalToken: A longer-lived token for session renewalpasswordExpired: Indicates if password change is requiredconditionToValidate: Indicates if user conditions need to be acceptedtwoFARequired: Indicates if 2FA is neededtwoFAValidated: Indicates if 2FA has been completed
Step 2: Handle Authentication Constraints
After initial authentication, the user may need to:
- Change expired password when response has
passwordExpiredset to true : Use/users/1.0/profile/password/changeendpoint - Accept user conditions when response has
conditionToValidateset to true: Use/users/1.0/profile/eulaendpoint - Complete 2FA when response has
twoFARequiredset to true : Use/users/1.0/session/upgradeendpoint withsecondFactorparameter
Step 3: Session Upgrade
- After resolving any constraints (password, conditions, 2FA), the user calls the upgrade endpoint
- Send a
GETrequest to/users/1.0/session/upgradewith optionalsecondFactorquery parameter - The expected response is
200with an updated JWT token when all conditions are met - When all conditions are met, the
twoFAValidatedflag is true and the user has full access
Step 4: Session Management
- Sign Out: Use
/users/1.0/session/signoutendpoint to invalidate all user sessions - Token Renewal: Use the
jwtRenewalTokento get a new authentication token without re-authentication
API endpoints used for user authentication
The API endpoints are relative to an API base URL that is provided via an environment variable. In a Nuxt.js implementation,
this variable is defined in the nuxt.config.ts file as follows:
export default defineNuxtConfig({
...
runtimeConfig: {
public: {
BACKEND_API_BASE: 'http://localhost:8091', // Backend API base URL
...
}
},
The best way to implement the interface with the backend is to use the libraries available here (in TypeScript for Nuxt.js):
/users/1.0/session/signin
Main authentication endpoint for user login.
- It does not require authentication, this endpoint is public
- It accepts a
POSTrequest with the login credentials in the body - The POST body structure and response structures are detailed in file user_login.md
- Returns
200on success with JWT tokens and authentication status - Returns
400on failure with error message (i18n key:user-login-refused)
/users/1.0/session/upgrade
Session upgrade endpoint to complete authentication process.
- It requires a valid JWT token with at least
ROLE_LOGIN_1FA - It accepts a
GETrequest with optionalsecondFactorquery parameter - Returns
200with updated JWT token when conditions are met - Returns
400on failure with error message
/users/1.0/session/signout
Sign out endpoint to invalidate user sessions.
- It requires a valid JWT token with
ROLE_REGISTERED_USER - It accepts a
GETrequest - Returns
200on success with confirmation message (i18n key:user-signed-out) - Returns
400on failure with error message
/users/1.0/profile/password/change
Password change endpoint for authenticated users.
- It requires a valid JWT token
- It accepts a
PUTrequest with old and new passwords - Returns
200on success with confirmation message (i18n key:user-profile-password-changed) - Returns
400on failure with error message
/users/1.0/profile/eula
User conditions validation endpoint.
- It requires a valid JWT token
- It accepts a
PUTrequest to accept the current user conditions - Returns
200on success with confirmation message (i18n key:user-profile-eula-accepted) - Returns
400on failure with error message
Security considerations
- Brute Force Protection: The backend implements brute force protection on login attempts per user and per IP
- JWT Token Security: All JWT tokens must be prefixed with
Bearerkeyword when used in API calls - Session Invalidation: Sign out invalidates all active sessions for the user
- No Error Details: For security reasons, authentication failures return generic error messages
This API endpoints are part of the IoT Tower Control backend, the base url should be an environment variable. You may ask developer for it when generating the code for the authentication page if you don't know it.