
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@christianacca/angular-swa-auth
Advanced tools
Programmatically work with Azure Static Web Apps authentication in an angular app
Programmatically work with Azure Static Web Apps authentication in an angular app
AuthService
facade
AuthGuard
SwaRoleGuard
SwaRoleCheckDirective
AutoLoginHttpInterceptor
IdentityProviderInteractiveSelectorService
Depending on your use case you might not need this library at all. You likely do NOT need this library if your requirements are simple:
If this is your app, then have a look at angular-swa-auth-nolib: a sample app that does not use this library to implement authentication
For all other use cases, this library will likely add value.
npm install @christianacca/angular-swa-auth
Import library in app module:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SwaAuthModule } from '@christianacca/angular-swa-auth';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
SwaAuthModule.forRoot({
// overrides to the default configuration here
})
],
bootstrap: [AppComponent]
})
export class AppModule {}
Implement login/logout/purge
The following is guidance only. For an alternative, where the user is prompted with a modal
to select the identity provider to sign in with see IdentityProviderInteractiveSelectorService
import { Component } from '@angular/core';
import { AuthService, ClientPrincipal } from '@christianacca/angular-swa-auth';
import { Observable } from 'rxjs';
@Component({
selector: 'app-auth',
template: `
<nav class="menu auth">
<p class="menu-label">Auth</p>
<div class="menu-list auth">
<ng-container *ngIf="userInfo$ | async as user; else loginTpl">
<a (click)="logout()">Logout</a>
<a (click)="purge()">Forget me</a>
</ng-container>
<ng-template #loginTpl>
<a *ngFor="let provider of providers" (click)="login(provider.id)">{{ provider.name }}</a>
</ng-template>
</div>
</nav>
`
})
export class AuthComponent {
userInfo$: Observable<ClientPrincipal | null>;
providers = this.authService.identityProviders;
constructor(private authService: AuthService) {
this.userInfo$ = this.authService.currentUser$;
}
login(identityProvider: string) {
this.authService.login({ identityProvider });
}
logout() {
this.authService.logout();
}
purge() {
this.authService.purge();
}
}
Optionally add AuthGuard
to your route(s)
If your app can only be accessed by authenticated users then add the guard to the top level route. EG:
import { AuthGuard } from '@christianacca/angular-swa-auth';
const routes: Route[] = [
{
path: '',
canActivate: [AuthGuard],
children: [
{
path: 'home',
component: ShellComponent,
children: [
// routes to your "content" pages in your app
]
},
{ path: '', pathMatch: 'full', redirectTo: '/home' }
]
}
];
Optionally add SwaRoleGuard
to your route(s)
import { AuthGuard } from '@christianacca/angular-swa-auth';
const routes: Route[] = [
{
path: 'product-admin',
data: {
allowedRoles: 'admin' // other ex: ['admin', 'owner'] ['admin', ['product-reader', 'owner']]
},
canActivate: [SwaRoleGuard],
component: AdminComponent
},
{
path: 'user-admin',
data: {
allowedRoles: 'owner'
},
canLoad: [SwaRoleGuard],
loadChildren: () => import('@christianacca/demo-app/user-admin').then(m => m.UserAdminModule)
}
];
Optionally send authentication session events to your function app api
imports: [
BrowserModule,
SwaAuthModule.forRoot({
sendSessionEventsToApi: true,
sessionEventsApiUrl: '/api/authevents' // this is the default if not supplied
})
]
IMPORTANT: you will need to add a function to your functions app api that receives via a POST an instance of AuthEventPayload
Install the Azure Static Web app emulator
npm i @azure/static-web-apps-cli -D
Add npm script to run your angular app
In package.json add the following to the scripts block:
{
"scripts": {
"start:swa": "swa start http://localhost:4200 --run \"npm start\""
}
}
Serve your app:
npm run start:swa
You can now browse to your app at: http:localhost:4280
Follow the official guidance here: https://docs.microsoft.com/en-us/azure/static-web-apps/get-started-portal?tabs=angular
FAQs
Programmatically work with Azure Static Web Apps authentication in an angular app
The npm package @christianacca/angular-swa-auth receives a total of 2 weekly downloads. As such, @christianacca/angular-swa-auth popularity was classified as not popular.
We found that @christianacca/angular-swa-auth demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.