Giter VIP home page Giter VIP logo

fusionauth-angular-sdk's Introduction

This repo is out of date and is archived. @fusionauth/angular-sdk is currently maintained at https://github.com/FusionAuth/fusionauth-javascript-sdk/tree/main/packages/sdk-angular

An SDK for using FusionAuth in Angular applications.

Table of Contents

Overview

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 new app.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.

There are 2 ways to interact with this SDK:

  1. By hosting your own server that performs the OAuth token exchange and meets the server code requirements for FusionAuth Web SDKs.
  2. By using the server hosted on your FusionAuth instance, i.e., not writing your own server code.

If you are hosting your own server, see server code requirements.

You can use this library against any version of FusionAuth or any OIDC compliant identity server.

Getting Started

Installation

NPM:

npm install @fusionauth/angular-sdk

Yarn:

yarn add @fusionauth/angular-sdk

Configuring FusionAuthModule

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 the server that performs the token exchange
      redirectUri: '', // The URI that the user is directed to after the login/register/logout action
    }),
  ],
  providers: [],
  bootstrap: []
})
export class AppModule { }

Usage

Pre-built buttons

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 {}

Service usage

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();
  }
}

State parameter

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.

Known Issues

None.

Quickstart

See the FusionAuth Angular Quickstart for a full tutorial on using FusionAuth and Angular.

Documentation

Full library documentation

Use backticks for code in this readme. This readme is included on the FusionAuth website, and backticks show the code in the best light there.

Releases

To perform a release to NPM, create a release on GitHub. That will automatically publish a release to GitHub.

Upgrade Policy

This library may periodically receive updates with bug fixes, security patches, tests, code samples, or documentation changes.

These releases may also update dependencies, language engines, and operating systems, as we'll follow the deprecation and sunsetting policies of the underlying technologies that the libraries use.

This means that after a dependency (e.g. language, framework, or operating system) is deprecated by its maintainer, this library will also be deprecated by us, and may eventually be updated to use a newer version.

fusionauth-angular-sdk's People

Contributors

colinfrick avatar david-chalk avatar davidprae avatar dmackinn avatar jakelo123 avatar lyleschemmerling avatar michaelyons avatar mooreds avatar vcampitelli avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fusionauth-angular-sdk's Issues

Add tests

Add tests

Description

The SDK has just boilerplate tests ("should create") at the moment. Let's add some 👏

image

I recommend testing FusionAuthService first.

Community guidelines

All issues filed in this repository must abide by the FusionAuth community guidelines.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.