An SDK for using FusionAuth in Angular applications.
This SDK allows you to add login, logout, and registration buttons to your Angular application. You can do this via pre-built buttons, or with the FusionAuthService in your own components.
Your users will be sent to FusionAuth’s themeable hosted login pages and then log in. After that, they are sent back to your Angular application.
Once authentication succeeds, the following secure, HTTP-only cookies will be set:
-
app.at
- an OAuth Access Token -
app.rt
- a Refresh Token used to obtain a newapp.at
. This cookie will only be set if refresh tokens are enabled on your FusionAuth instance.
The access token can be presented to APIs to authorize the request and the refresh token can be used to get a new access token.
Note that this SDK requires you to have a server that performs the OAuth token exchange. See Server Code Requirements for more details.
You can use this library against any version of FusionAuth or any OIDC compliant identity server.
NPM:
npm install @fusionauth/angular-sdk
Yarn:
yarn add @fusionauth/angular-sdk
To configure the SDK, wrap your app with FusionAuthProvider
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { FusionAuthModule } from '@fusionauth/angular-sdk';
@NgModule({
declarations: [],
imports: [
FusionAuthModule.forRoot({
clientId: '', // Your FusionAuth client ID
serverUrl: '', // The base URL of your server for the token exchange
redirectUri: '', // The URI that the user is directed to after the login/register/logout action
}),
],
providers: [],
bootstrap: []
})
export class AppModule { }
Authenticating with FusionAuth requires you to set up a server that will be used to perform the OAuth token exchange. This server must have the following endpoints:
This endpoint must:
- Generate PKCE code. a. The code verifier should be saved in a secure HTTP-only cookie. b. The code challenge is passed along
- Encode and save
redirect_url
from angular app tostate
. - Redirect browser to
/oauth2/authorize
with aredirect_uri
to/app/token-exchange
This endpoint must:
-
Call /oauth2/token to complete the Authorization Code Grant request. The
code
comes from the request query parameter andcode_verifier
should be available in the secure HTTP-only cookie, while the rest of the parameters should be set/configured on the server side. -
Once the token exchange succeeds, read the
app.at
from the response body and set it as a secure, HTTP-only cookie with the same name. -
If you wish to support refresh tokens, repeat step 2 for the
app.rt
cookie. -
Save the expiration time in a readable
app.at_exp
cookie. And save theapp.idt
id token in a readable cookie. -
Redirect browser back to encoded url saved in
state
. -
Call /oauth2/userinfo to retrieve the user info object and respond back to the client with this object.
This endpoint is similar to /login
. It must:
- Generate PKCE code. a. The code verifier should be saved in a secure HTTP-only cookie. b. The code challenge is passed along
- Encode and save
redirect_url
from angular app tostate
. - Redirect browser to
/oauth2/register
with aredirect_uri
to/app/callback
This endpoint must:
- Use
app.at
from cookie and use as the Bearer token to call/oauth2/userinfo
- Return json data
This endpoint must:
- Clear the
app.at
andapp.rt
secure, HTTP-only cookies. - Clear the
app.at_exp
andapp.idt
secure cookies. - Redirect to
/oauth2/logout
This endpoint is necessary if you wish to use refresh tokens. This endpoint must:
-
Call /oauth2/token to get a new
app.at
andapp.rt
. -
Update the
app.at
,app.at_exp
,app.idt
, andapp.rt
cookies from the response.
There are three pre-styled buttons that are configured to perform login/logout/registration. They can be placed anywhere in your app as is.
import { Component } from '@angular/core';
@Component({
selector: 'app-login',
template: `<fa-login></fa-login>`,
styleUrls: []
})
export class LoginComponent {}
@Component({
selector: 'app-logout',
template: `<fa-logout></fa-logout>`,
styleUrls: []
})
export class LogoutComponent {}
@Component({
selector: 'app-register',
template: `<fa-register></fa-register>`,
styleUrls: []
})
export class RegisterComponent {}
Alternatively, you may interact with the SDK Service by injecting the FusionAuthService into any component or service.
import { Component, OnInit } from '@angular/core';
import { FusionAuthService, UserInfo } from '@fusionauth/angular-sdk';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
private userInfo: UserInfo;
constructor(
private fusionAuth: FusionAuthService,
) {}
async ngOnInit(): Promise<void> {
this.fusionAuth.initAutoRefresh();
}
login() {
this.fusionAuth.startLogin();
}
register() {
this.fusionAuth.startRegistration();
}
logout() {
this.fusionAuth.logout();
}
refreshToken() {
this.fusionAuth.refreshToken();
}
async getUserInfo() {
this.userInfo = await this.fusionAuth.getUserInfo();
}
isLoggedIn(): boolean {
return this.fusionAuth.isLoggedIn();
}
}
The startLogin
and startRegistration
functions both accept an optional string
parameter called state
. The login and register components can also be passed the
state as an input. The state that is passed in to the function call will be echoed
back in the state query parameter of the callback uri specified in redirectUri
on
the FusionAuthConfig
used to initialize the FusionAuthModule
. Though you may
pass any value you would like for the state parameter, it is often used to indicate
which page the user was on before redirecting to login or registration, so that the
user can be returned to that location after a successful authentication.
See the FusionAuth Angular SDK Example for functional example of a Angular client that utilizes the SDK as well as an Express server that performs the token exchange.
Use backticks for code in this readme. This readme gets turned into asciidoc and included on the fusionauth website, and backticks show the code in the best light there.
To perform a release:
-
Pull the code to your local machine
-
Bump the version in package.json
-
Run
npm run build-sdk
-
Run
cd dist/fusionauth-angular-sdk
-
Run
npm publish
You may have to set up your machine to be able to publish, which will involve updating your .npmrc file.
There’s information here that you can
use
to do that (look for the .npmrc
section).