【问题标题】:Laravel vue3 sanctum fortify on api/user unauthenticatedLaravel vue3 sanctum fortify on api/user unauthenticated
【发布时间】:2021-11-04 02:42:57
【问题描述】:

我正在使用 laravel sanctum 以及 laravel fortify。我可以登录,但是当我获取 mydomain.test/api/user 时,我得到 401(未经授权)。

.env

APP_URL=http://curriculum.test
SANCTUM_STATEFUL_DOMAINS=curriculim.test,localhost,127.0.0.1
SESSION_DOMAIN=.curriculim.test
SESSION_DRIVER=cookie

cors.php

'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

这是我的登录方法

async login({ dispatch }, payload) {
            try {
                await axios.get('/sanctum/csrf-cookie');

                await axios.post('/api/login', payload).then((res) => {
                    console.log('successfully logged in')
                    return dispatch('getUser');
                }).catch((err) => {
                    throw err.response
                });


            } catch (e) {
                throw e
            }

        },

        async getUser({commit}) {
            await axios.get('/api/user').then((res) => {
            commit('setUser', res.data);
          }).catch((err) => {

          })
        },

【问题讨论】:

    标签: laravel vue.js laravel-sanctum


    【解决方案1】:

    检查

    • 是同一域中的cookie(例如.domain.com)
    • 在第二个 axios 请求中添加“withCredentials”
    • 可能是 CORS 错误?检查您的 CORS 配置

    【讨论】:

    • 在我的 app.js 中。我有 axios.defaults.withCredentials = true;
    猜你喜欢
    • 1970-01-01
    • 2021-05-31
    • 2021-03-14
    • 2022-07-03
    • 1970-01-01
    • 2022-07-11
    • 2017-12-22
    • 2019-11-24
    • 2021-01-10
    相关资源
    最近更新 更多