Home > Web Front-end > uni-app > How does uniapp check whether the user is logged in?

How does uniapp check whether the user is logged in?

coldplay.xixi
Release: 2020-12-17 10:24:08
Original
8612 people have browsed it

Uniapp's method of checking whether the user is logged in: first open the [user.vue] file in the APP code; then use uni's setstorage API to obtain the value. If the value is obtained, it means that the user is logged in. If you do not get the value, You will not be able to jump to the login interface.

How does uniapp check whether the user is logged in?

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version. This method is suitable for all brands of computers.

Recommended (free): uni-app development tutorial

Uniapp method to check whether the user is logged in:

Open us The user.vue file in the APP code:

How does uniapp check whether the user is logged in?

##Implementing logic

When we determine whether the user is logged in, we need to store a value into the client. If this value exists, it means the user is logged in, if not, the user needs to log in. So the first logic we need to do is: use uni's setstorage API to get the value. If you get the value, it means you are logged in. If you don't get the value, you will not be able to jump to the login interface.

Logical Coding

<template>
<view></view>
</template>
<script>
export default {
data() {
return {};
},
//uni生命周期函数,页面显示就执行,这意味着如果一打开这个页面,如果没登录将永远跳转到登录页面
onShow() {
/****************************************************用户是否登录代码开始:https://www.clearnull.com/963.html*****/
//uni获取本地数据API
uni.getStorage({
key: &#39;token&#39;, //数据key值,也就是你存储数据时的名称
success: function(res) {
//数据成功获取,用户已登录
console.log(res.data);
},
fail: function(res) {
//数据未获取成功,用户没有登录,这里我们直接跳转到登录页面
uni.navigateTo({
url: &#39;../login/login&#39;,
});
}
});
/*************************************用户是否登录代码结束**********************/
}
};
</script>
<style></style>
language-javascript
Copy after login

How does uniapp check whether the user is logged in?

After saving the code and running it in the browser, you will find that as long as you click on the user interface, you will jump to the login page, because at this time It's worthless.

Related free learning recommendations:

php programming (video)

The above is the detailed content of How does uniapp check whether the user is logged in?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template