253 Attaching the Token to Outgoing Requests

步骤

1、auth/getters.js中添加获取token的getter

 token(state) {
    return state.token;
},

2、coaches/actions.js添加token

import axios from "axios";

export default {
    async registerCoach(context, payload) {
        const userId = context.rootGetters.userId;

        const newCoach = {
            firstName: payload.firstName,
            lastName: payload.lastName,
            description: payload.description,
            hourlyRate: payload.rate,
            areas: payload.areas,
        };

        // const response = await fetch(`http://localhost:5216/api/PutCoach/${userId}`,{
        //     method:'PUT',
        //     body: JSON.stringify(newCoach),
        //     headers: {
        //         'Content-Type': 'application/json',
        //     }
        // });
        const token = context.rootGetters.token;

        axios.defaults.headers.common['Content-Type'] = 'application/json';
        const response = await axios.post(`http://localhost:5216/api/Coaches/PostCoach?auth=${token}`, {
            ...newCoach,
            Id: userId
        }).then((response) => {
            console.log(response);
        }).catch((error) => {
            console.log(error);
        });
        //const responseData = await response.json();

        

        context.commit('registerCoach', {
            ...newCoach,
            id: userId
        });
    },
    async loadCoaches(context, payload) {
        if(!payload.forceRefresh && !context.getters.shouldUpdate){
            return;
        }
        console.log('shouldUpdate');

        const response = await fetch('http://localhost:5216/api/Coaches/GetCoaches');
        const responseData = await response.json();
        console.log(responseData);

        if (!response.ok){
            //error ...
            const error = new Error(responseData.message || 'Failed to fetch!');
            throw error;
        }

        const coaches = [];
        for(const key in responseData){
            const coach = {
                id: responseData[key].id,
                firstName: responseData[key].firstName,
                lastName: responseData[key].lastName,
                description: responseData[key].description,
                hourlyRate: responseData[key].hourlyRate,
                areas: responseData[key].areas
            };
            coaches.push(coach);
        }
        context.commit('setCoaches', coaches);

        context.commit('setFetchTimestamp');
    }
};

3、requests/actions.js添加token


import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';

export default {
    async contactCoach(context, payload) {
        const newRequest = {
            id: uuidv4(),
            coachId: payload.coachId,
            userEmail: payload.email,
            message: payload.message,
        }

        const response = await axios.post('http://localhost:5216/api/Requests/PostRequest', newRequest)
            .then((res) => {
                console.log(res);
            })
            .catch((err) => {
                console.log(err);
            });


        context.commit('addRequest', newRequest);
    },

    async loadRequests(context, payload) {
        const coachId = context.rootGetters.userId;
        const token = context.rootGetters.token;
        const response = await fetch(`http://localhost:5216/api/Requests/GetRequets/${coachId}?auth=${token}`);
        const responseData = await response.json();
        console.log(responseData);

        if (!response.ok){
            //error ...
            const error = new Error(responseData.message || 'Failed to fetch!');
            throw error;
        }

        const requests = [];
        for(const key in responseData){
            const request = {
                id: responseData[key].id,
                userEmail: responseData[key].userEmail,
                message: responseData[key].message,
                coachId: responseData[key].coachId,
            };
            requests.push(request);
        }
        context.commit('setRequests', requests);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值