platform/README.md

83 lines
2.5 KiB
Markdown
Raw Normal View History

2016-05-02 23:39:57 +00:00
# angel_auth
2016-11-23 20:37:40 +00:00
2017-12-07 06:27:30 +00:00
[![Pub](https://img.shields.io/pub/v/angel_auth.svg)](https://pub.dartlang.org/packages/angel_auth)
2018-07-10 23:32:34 +00:00
[![build status](https://travis-ci.org/angel-dart/auth.svg?branch=master)](https://travis-ci.org/angel-dart/auth)
2016-11-23 20:37:40 +00:00
2016-05-02 23:39:57 +00:00
A complete authentication plugin for Angel. Inspired by Passport.
2016-09-21 06:19:52 +00:00
2017-06-03 21:39:55 +00:00
# Wiki
2016-11-23 20:37:40 +00:00
[Click here](https://github.com/angel-dart/auth/wiki).
2016-09-21 06:19:52 +00:00
2017-06-03 21:39:55 +00:00
# Bundled Strategies
2016-12-03 18:23:11 +00:00
* Local (with and without Basic Auth)
2017-06-03 21:39:55 +00:00
* Find other strategies (Twitter, Google, OAuth2, etc.) on Pub!!!
# Example
Ensure you have read the [wiki](https://github.com/angel-dart/auth/wiki).
```dart
configureServer(Angel app) async {
var auth = new AngelAuth();
auth.serializer = ...;
auth.deserializer = ...;
2018-09-11 22:12:15 +00:00
auth.strategies['local'] = new LocalAuthStrategy(...);
2017-06-03 21:39:55 +00:00
// POST route to handle username+password
app.post('/local', auth.authenticate('local'));
// Use a comma to try multiple strategies!!!
//
// Each strategy is run sequentially. If one succeeds, the loop ends.
// Authentication failures will just cause the loop to continue.
//
// If the last strategy throws an authentication failure, then
// a `401 Not Authenticated` is thrown.
var chainedHandler = auth.authenticate(
2017-09-24 04:32:38 +00:00
['basic','facebook'],
2017-06-03 21:39:55 +00:00
authOptions
);
2017-09-24 04:32:38 +00:00
// Apply angel_auth-specific configuration
await app.configure(auth.configureServer);
// Middleware to decode JWT's...
app.use(auth.decodeJwt);
2017-06-03 21:39:55 +00:00
}
```
2017-02-28 22:16:25 +00:00
# Default Authentication Callback
A frequent use case within SPA's is opening OAuth login endpoints in a separate window.
[`angel_client`](https://github.com/angel-dart/client)
provides a facility for this, which works perfectly with the default callback provided
in this package.
```dart
2017-06-03 21:39:55 +00:00
configureServer(Angel app) async {
var handler = auth.authenticate(
'facebook',
new AngelAuthOptions(callback: confirmPopupAuthentication()));
app.get('/auth/facebook', handler);
// Use a comma to try multiple strategies!!!
//
// Each strategy is run sequentially. If one succeeds, the loop ends.
// Authentication failures will just cause the loop to continue.
//
// If the last strategy throws an authentication failure, then
// a `401 Not Authenticated` is thrown.
var chainedHandler = auth.authenticate(
2017-09-24 04:32:38 +00:00
['basic','facebook'],
2017-06-03 21:39:55 +00:00
authOptions
);
}
2017-02-28 22:16:25 +00:00
```
2017-06-03 21:39:55 +00:00
2017-04-12 12:03:31 +00:00
This renders a simple HTML page that fires the user's JWT as a `token` event in `window.opener`.
`angel_client` [exposes this as a Stream](https://github.com/angel-dart/client#authentication):
```dart
app.authenticateViaPopup('/auth/google').listen((jwt) {
// Do something with the JWT
});
```