Tudor-Radu Barbu
Tudor-Radu Barbu

Reputation: 494

How to handle API credentials in an Angular app?

I created an API in Laravel and I have a small SPA in Angular that logins in the app via an username & password and receive a token. With this token you can do some basic stuff with the API.

It's not ok to save the credentials (user&pass) in the Angular app (plain text - javascript etc etc). How can I handle this kind of auth in Javascript? What is a best practice?

Upvotes: 1

Views: 704

Answers (1)

LorenzoBerti
LorenzoBerti

Reputation: 6974

After login, You can return the token and store in a localstorage for future use and refresh, and if you don't want put your token in every ajax call manually you can add to $http like:

$http.defaults.headers.common['Authorization'] = 'Bearer ' + storage.token;

I have tried this with angularjs and laravel with this library: https://github.com/tymondesigns/jwt-auth

Upvotes: 1

Related Questions