React项目框架搭建(CRA版本搭建)二

第二章:登录页面以及token存取

创建登录页面Login

/src/pages/login/index.tsx

我这边简单复制下antd示例过来

import { Form, Input, Button, Checkbox, Card } from "antd";

const Login = () => {
  const onFinish = (values: any) => {
    console.log("Success:", values);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <Form
      name="basic"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="账号"
        name="username"
        rules={[{ required: true, message: "Please input your username!" }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="密码"
        name="password"
        rules={[{ required: true, message: "Please input your password!" }]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item
        name="remember"
        valuePropName="checked"
        wrapperCol={{ offset: 8, span: 16 }}
      >
        <Checkbox>记住账号</Checkbox>
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
        <Button type="primary" htmlType="submit">
          登录
        </Button>
      </Form.Item>
    </Form>
  );
};

export default Login;

我们在根目录下的index.tsx使用Login组件

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import './style/index.less'
import Login from './pages/login';

ReactDOM.render(
  <React.StrictMode>
    <Login />
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

yarn start下运行后看到登录界面

界面完后,需要考虑登录逻辑,token的存取,这里使用react-cookies存储token

引入react-cookies

npm install react-cookie --save
// or
yarn add react-cookie

react-cookies api简单介绍

// 引入
import cookie from 'react-cookies'
// 存
cookie.save(‘token’, "123”,{});
// 取
cookie.load('token’);
// 删除
cookie.remove('token')
// 存数据时设置失效时间
const oneDay = new Date(new Date().getTime() + 24 * 3600 * 1000); 
// 设置失效日期一天
cookie.save("token", "ffffffffff", { expires: oneDay });

我们这里直接在登录成功后调用存token,并且设置失效日期为一天

const onFinish = (values: any) => {
    console.log("Success:", values);
    console.log("cookie:", cookie.load("token"));

    // 登录成功
    setTimeout(() => {
      const oneDay = new Date(new Date().getTime() + 24 * 3600 * 1000); // 设置失效日期一天
      cookie.save("token", "ffffffffff", { expires: oneDay });
    }, 1000);
  };

提示:token是否需要存可以与后端沟通,做的好几个项目都不需要前端对token存储,后端直接存session里面,如果不需要可以不引入react-cookies。

到这里登陆界面与token就完成了,贴下本次的完整的登陆界面代码

import { Form, Input, Button, Checkbox, Card } from "antd";
import cookie from "react-cookies";

const Login = () => {
  const onFinish = (values: any) => {
    console.log("Success:", values);
    console.log("cookie:", cookie.load("token"));

    // 登录成功
    setTimeout(() => {
      const oneDay = new Date(new Date().getTime() + 24 * 3600 * 1000); // 设置失效日期一天
      cookie.save("token", "ffffffffff", { expires: oneDay });
    }, 1000);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <Card style={{ top: 50, width: "50%", marginLeft: "25%" }}>
      <Form
        name="basic"
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 18 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item
          label="账号"
          name="username"
          rules={[{ required: true, message: "Please input your username!" }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="密码"
          name="password"
          rules={[{ required: true, message: "Please input your password!" }]}
        >
          <Input.Password />
        </Form.Item>

        <Form.Item
          name="remember"
          valuePropName="checked"
          wrapperCol={{ offset: 8, span: 16 }}
        >
          <Checkbox>记住账号</Checkbox>
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
    </Card>
  );
};
export default Login;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容