Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Authentication (2019) Implementing Basic Authentication Implement Sign In

signin 401 error

I keep getting a 401 unauthorized error for the sign-in.I have compared my code with the lesson and can't find any difference, please advice

` // context const value = { data: this.data, actions: { signIn: this.signIn, }, };

// user sign in

submit = () => { const { context } = this.props; const { username, password } = this.state; context.actions .signIn(username, password) .then((user) => { if (user === null) { this.setState(() => { return { errors: ["Sign-in was unsuccessful"] }; }); } else { this.props.history.push("/authenticated"); console.log(SUCCESS! ${username} is now signed in!); } }) .catch((err) => { console.log(err); this.props.history.push("/error"); }); };

cancel = () => { this.props.history.push("/"); };

// get user

async getUser() { const response = await this.api(/users, "GET", null, true, { username, password, }); if (response.status === 200) { return response.json().then((data) => data); } else if (response.status === 401) { return null; } else { throw new Error(); } } `

2 Answers

I doubt this will help now but I had the same issue and for me the reason was in the Data.js file for the async getUsers() function I forgot to add the parameters for username and password so it should look like the following:

async getUser(username, password) { ... }

After adding this and restarting the client and the api my issue was resolved. Hope this helps anyone in the future!

I got the same error. Not sure if you fixed it already but the reason I was getting that error was that in the Data.js file at the sign-in function, I was passing in the username and password object wrong. I had: {username, password} instead of {username: username.value, password: password.value}. I hope this helps!