<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Al Meshaal</title>
    <description>The latest articles on DEV Community by Ahmed Al Meshaal (@ahmedmeshaal).</description>
    <link>https://dev.to/ahmedmeshaal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1192808%2F2f915c9c-1104-4fc0-85ad-162c36eef097.png</url>
      <title>DEV Community: Ahmed Al Meshaal</title>
      <link>https://dev.to/ahmedmeshaal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedmeshaal"/>
    <language>en</language>
    <item>
      <title>AxiosError: Request failed with status code 401 in ReactJS</title>
      <dc:creator>Ahmed Al Meshaal</dc:creator>
      <pubDate>Tue, 24 Oct 2023 11:37:37 +0000</pubDate>
      <link>https://dev.to/ahmedmeshaal/axioserror-request-failed-with-status-code-401-in-reactjs-1oc8</link>
      <guid>https://dev.to/ahmedmeshaal/axioserror-request-failed-with-status-code-401-in-reactjs-1oc8</guid>
      <description>&lt;p&gt;I am working on React authentication app, but cannot login successfully or and cannot generate access token.&lt;/p&gt;

&lt;p&gt;could you help me please I didn't found any solution on the internet.&lt;/p&gt;

&lt;p&gt;Chack my code &lt;br&gt;
The Login Service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';
const API_BASE_URL = 'http://accrdet.localhost';

export default class LoginServices {
    login(username, password) {
        return axios.post(`${API_BASE_URL}/api/auth/login`, {
            username,
            password
        },
        {
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        }
            ).then(response =&amp;gt; {
            if (response.data.accessToken) {
                localStorage.setItem('user', JSON.stringify(response.data));
            }
            return response.data;
        });
    }

    logout() {
        return axios.post(`${API_BASE_URL}/api/auth/logout`, {}).then(response =&amp;gt; {
            if (response.data.accessToken) {
                localStorage.removeItem('user', JSON.stringify(response.data))
            }
            return response.data;
        })
    }

    geCurrentUser(){
        return JSON.parse(localStorage.getItem('user'));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Login Component is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import LoginService from '../../services/LoginServices';

const auth_service = new LoginService()

class Login extends Component {
    constructor(props) {
        super(props);

        this.handleLogin = this.handleLogin.bind(this);
        this.onChangeUsername = this.onChangeUsername.bind(this);
        this.onChangePassword = this.onChangePassword.bind(this);

        this.state = {
            username: "",
            password: ""
        };
    }

    onChangeUsername(e) {
        this.setState({
            username: e.target.value
        });
    }

    onChangePassword(e) {
        this.setState({
            password: e.target.value
        });
    }

    // Login handler
    handleLogin(e) {
        e.preventDefault();

        auth_service.login(this.state.username, this.state.password).then(
            (response) =&amp;gt; {
                const token = response.key;
                const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
                localStorage.setItem('token', token);
                localStorage.setItem('expirationDate', expirationDate);
                console.log(`Token: ${token}`);
            }, error =&amp;gt; {
                console.log(`Eoor -&amp;gt; ${error}`);
            }
        )
    }


    render() {
        return (
        &amp;lt;&amp;gt;
            &amp;lt;form onSubmit={this.handleLogin} className="form-signin mt-3"&amp;gt;
                &amp;lt;div className="from-control"&amp;gt;
                    &amp;lt;label htmlFor="usenrame"&amp;gt;Username&amp;lt;/label&amp;gt;
                    &amp;lt;input type="text"
                           value={this.state.username}
                           onChange={this.onChangeUsername}
                           name="usenrame"
                           id="username"
                           className="form-control mb-2"
                           placeholder="username"
                    /&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div className="from-control"&amp;gt;
                    &amp;lt;label htmlFor="password"&amp;gt;Password&amp;lt;/label&amp;gt;
                    &amp;lt;input type="password"
                           value={this.state.password}
                           onChange={this.onChangePassword}
                           name="password"
                           id="password"
                           className="form-control mb-2"
                           placeholder="********" required
                    /&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;button className="btn btn-sm btn-primary mr-5 mb-1" type="submit"&amp;gt;Sign in&amp;lt;/button&amp;gt;
           &amp;lt;/form&amp;gt;

        &amp;lt;/&amp;gt;
        );
    }
}

export default Login;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
