Coder Social home page Coder Social logo

pwdm-frontend's Introduction

个人项目 pwd manager 的前端页面,选用了 amis 前端低代码框架。

使用的是 amis sdk,版本 3.4.3

JWT 身份验证

引入 axios cdn

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

login.html

在amis.embed的第四个参数中这样写

fetcher: (url, method, data, config) => {
// 发起请求使用 axios
    return axios(url, method, data, config)
    .then(response => {
        if (response.data.status === 0) {
            // 提取JWT Token,假设Token存储在响应的data字段中
            const token = response.data.data.token;

            if (token) {
                // 将Token存储在localStorage中
                localStorage.setItem('token', token);
                console.log(token);
            }
        }
        return response; // 返回响应对象
    })
    .catch(error => {
        // 处理请求错误
        console.error('请求错误', error);
        throw error;
});

index.html

在每个请求中都加入

"headers": {
	"token": "${ls:token}"
}

例如:

"api": {
    "url": "/api/account/page",
    "method": "get",
    "headers": {
        "token": "${ls:token}"
    }
}

在amis.embed的第四个参数中这样写,我这里设置的 jwt 验证不通过后端返回401

fetcher: (url, method, data, config) => {
    const token = localStorage.getItem("token");
    // 如果token不存在,跳转index.html
    if (!token) {
        window.location.href = '/login.html';
    }

    return axios(url, method, data, config)
        .catch(error => {
            if (error.response && error.response.status === 401)
                window.location.href = '/login.html';
        });
}

pwdm-frontend's People

Contributors

littlejackey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.