This is an example app that shows how to use Auth0 with Angular 2. It uses Auth0's angular2-jwt module.
npm install
If you haven't already done so, sign up for your free Auth0 account. Once you have the client ID and client domain for your app, replace the argumentts in Auth0Lock
within auth.service.ts
with them.
// auth.service.ts
...
lock = new Auth0Lock('YOUR_AUTH0_CLIENT_ID', 'YOUR_AUTH0_DOMAIN');
...
npm start
The app will be served at localhost:9000
.
The best way to have authentication utilities available across the application is to use an Injectable
service. This is provided in auth.service.ts
and is the spot where Auth0Lock is set up.
// auth.service.ts
import {Injectable, NgZone} from 'angular2/core';
import {Router} from 'angular2/router';
import {AuthHttp, tokenNotExpired} from 'angular2-jwt';
// Avoid name not found warnings
declare var Auth0Lock: any;
@Injectable()
export class Auth {
lock = new Auth0Lock('YOUR_AUTH0_CLIENT_ID', 'YOUR_AUTH0_DOMAIN');
refreshSubscription: any;
user: Object;
zoneImpl: NgZone;
...
The AuthHttp
class is used to automatically attach the user's JWT as an Authorization
header when making requests. Making secure HTTP calls looks the same as it would with regular Http
.
// ping.component.ts
...
securedPing() {
this.authHttp.get(`${this.API_URL}/secured/ping`)
.map(res => res.json())
.subscribe(
data => this.message= data.text,
error => this.message = error._body
);
}
...
There is a simple NodeJS server in the server
directory. See the readme there for instructions on starting it.
Auth0 helps you to:
- Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
- Add authentication through more traditional username/password databases.
- Add support for linking different user accounts with the same user.
- Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
- Analytics of how, when and where users are logging in.
- Pull data from other sources and add it to the user profile, through JavaScript rules.
- Go to Auth0 and click Sign Up.
- Use Google, GitHub or Microsoft Account to login.
This project is licensed under the MIT license. See the LICENSE file for more info.