JS uses session, cookie, angular cookie to save tokens

Source: Internet
Author: User
Tags sessionstorage

Directory

1. Preface

2, JS front-end use session to save tokens

3, JS front-end use cookies to save tokens

4. JS front end use angular cookie to save token

Content

1. Preface

When the front-end API interface is requested for security purposes, it is generally necessary for the user to log on successfully before other requests can be sent.

Therefore, after the user login is successful, the background will return a token to the front end, this time we need to put tokens temporarily in the local, each time the request to send a token in the header, the local token and the background database token for a verification , if the two are consistent, the request succeeds, or the request fails.

For example, return tokens after successful login:

  

As shown, take tokens in the header when making other requests:

  

2, JS front-end use session to save tokens

The Sessionstorage property allows you to access a session storage object, which is similar to Localstorage, except that the data stored in the Localstorage does not have an out-of-date setting. The data stored in the Sessionstorage will be erased at the end of the page session. A page session remains in the browser while it is open, and reloading or resuming the page still maintains the original page session.

Here is the specific syntax for storing tokens in the session:

//save data to SessionstorageSessionstorage.setitem ('Key','value');//getting data from Sessionstoragevardata = Sessionstorage.getitem ('Key');//Delete saved data from SessionstorageSessionstorage.removeitem ('Key');//Delete all saved data from SessionstorageSessionstorage.clear ();

3, JS front-end use cookies to save tokens

The token is saved in a cookie and the token in the cookie is emptied once the browser is closed.

Here is the syntax for storing tokens in a cookie:

// save tokens in a cookie document.cookie=token; // Remove tokens from cookies var token = document.cookie.split ("; ") [0];

4. JS front end use angular cookie to save token

The introduction of cookies in angular must first introduce the Angular-cookies.js file. The following is a link to a version that can be opened directly in the browser: https://cdn.bootcss.com/angular-cookie/4.1.0/angular-cookie.js

The following is the syntax for storing tokens in a angular cookie:

//Store Tokensangular. Module ('Theme.demos.login_page', ['ngcookies']). Controller ('Logincontroller'['$scope','$cookieStore', function ($scope, $cookieStore) {varvalueindex=encodeURI (Data.data.token); $cookieStore. Put ('Tokenindex', ValueIndex);})//Get token .angular. Module ('Theme.demos.login_page', ['ngcookies']). Controller ('Logincontroller'['$scope','$cookieStore', function ($scope, $cookieStore) {varToken= $cookieStore.Get('Tokenindex');})

These are the three more common methods of storing tokens in JS. And these three methods are compatible in Google Chrome, Firefox browser, IE browser, but there is an incompatibility problem (Apple) in the Safari browser, it is not possible to write tokens to the cookie.

JS uses session, cookie, angular cookie to save tokens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.