O
O
Ostap2020-05-19 00:42:54
JavaScript
Ostap, 2020-05-19 00:42:54

Correct authorization?

Hello. I ask for help. I make a request for authorization and receiving a token to the server https://fan-foto-ml753mfuuq-uc.a.run.app/ (login method). I make a request through Fetch.
Initially got ' Access to fetch at ' https://fan-foto-ml753mfuuq-uc.a.run.app/api/v1/lo... ' from origin ' localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource .
Installed a CORS app for Chrome and now I'm getting " Unhandled Rejection (Error): Not Fetch /api/v1/login/access-token , recived 405 "


_apiBase = 'https://fan-foto-ml753mfuuq-uc.a.run.app';
    proxyurl = "https://cors-anywhere.herokuapp.com/";

    getResource = async (url) => {
        const res = await fetch(`${this._apiBase}${url}`);
        if (!res.ok) {
            throw new Error(`Not Fetch ${url} , recived ${res.status}`)
        }
        const body = await res.json();
        return body;
    };

    getToken = async (username, password) => {
        let user = {
            username,
            password
        };
        const token = await this.getResource(`/api/v1/login/access-token`, {
                // method: 'POST',
                method: 'OPTIONS',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer TOKEN',
                    // 'Access-Control-Allow-Origin': 'http://127.0.0.1:3000',
                    // 'Access-Control-Allow-Methods': 'GET, POST, HEAD, OPTIONS',
                    // 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
                    // 'Access-Control-Allow-Credentials': true

                },
                // credentials: true,
                // origin: 'http://127.0.0.1:3000',
                // accept: 'application/json',
                body: JSON.stringify(user)
            }
        );
        return token;

    };


I've already tried everything I know. Please help. Thanks

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dasha Tsiklauri, 2020-05-19
@dasha_programmist

so you take getResource = async (url) one argument, and you pass two , and the second one, of course, does not pass
this.getResource(`/api/v1/login/access-token`, {

S
Sergey Karbivnichy, 2020-05-19
@hottabxp

405 error - because you are using the OPTIONS method, but you most likely need POST.

K
Konstantin, 2020-05-19
@jcricket

Try to specify one of the settings in headers: cors, no-cors, same-origin or navigate.
https://developer.mozilla.org/en/docs/Web/API/Requ...

V
VegasChickiChicki, 2020-05-19
@VegasChickiChicki

We work locally - turn on .
We stop working - turn it off (some sites stop working with this plugin).
On the server, the error related to CORS will disappear.
At least that's how it was for me.

M
mr_Dick, 2020-05-19
@mr_Dick

mode: 'no-cors', credentials: 'include', . Try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question