https://github.com/zurfyx/angular2-http-auth
Angular 5 authenticated HTTP requests.
https://github.com/zurfyx/angular2-http-auth
angular angular5 auth http
Last synced: 2 months ago
JSON representation
Angular 5 authenticated HTTP requests.
- Host: GitHub
- URL: https://github.com/zurfyx/angular2-http-auth
- Owner: zurfyx
- License: mit
- Created: 2017-05-15T13:14:56.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-11T10:29:50.000Z (over 7 years ago)
- Last Synced: 2025-01-22T02:36:12.399Z (4 months ago)
- Topics: angular, angular5, auth, http
- Language: TypeScript
- Homepage: https://zurfyx.github.io/angular2-http-auth
- Size: 845 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Angular HTTP Auth
[](https://www.npmjs.com/package/angular2-http-auth)
[](https://travis-ci.org/zurfyx/angular2-http-auth)> Angular authenticated HTTP requests (no JWT required).
Inspired by [angular2-jwt](https://github.com/auth0/angular2-jwt)
## Install
```
npm install angular2-http-auth
```## Usage
[auth-http.module.ts](https://github.com/zurfyx/angular2-http-auth/blob/master/example/app/auth-http.module.ts)
This is the configuration file. You can pass it default fixed headers as well as dynamic headers (such as the authorization token).
Make sure to import this module prior doing any authenticated HTTP call.
`app.module.ts` is often a good place to import it.
```
import { AuthHttp, AuthConfig } from 'angular2-http-auth';export function authHttpServiceFactory(
http: Http,
tokenIsHereService: OptionalServiceThatProvidesTheAuthorizationToken,
) {
return new AuthHttp(new AuthConfig({
headers: {
'Content-Type': 'application/json',
Authorization: () => tokenIsHereService.authorizationKey,
},
}), http);
}@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, OptionalServiceThatProvidesTheAuthorizationToken]
}
]
})
export class AuthHttpModule { }
```*It is very similar to [angular2-jwt](https://github.com/auth0/angular2-jwt)'s, even though some parameters might be different.*
[app.module.ts](https://github.com/zurfyx/angular2-http-auth/blob/master/example/app/app.module.ts)
```
import { HttpModule } from '@angular/http';
import { AuthHttpModule } from './auth-http.module';@NgModule({
imports: [
...
HttpModule,
AuthHttpModule,
...
],
```[my-user-panel.service.ts](https://github.com/zurfyx/angular2-http-auth/blob/master/example/app/app.service.ts)
```
import { AuthHttp } from 'angular2-http-auth';
...
fetchPrivateStuff(): Observable {
return this.authHttp.post(`https://my-site.com/privateStuff`, {});
}
```## TODO
* Default headers
* Default search params
* Default body
* withCredentials (cookies)## License
MIT © [Gerard Rovira Sánchez](//zurfyx.com)