Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi all! I like to display user's name and picture on navigation bar & home page once user log in. React 1. log in button

Hi all!

I like to display user's name and picture on navigation bar & home page once user log in.

React

1. log in button to log out button once user logged in,

2. display user's name on navigation bar next to log out button (maybe have drop bar option to edit profile and logout??)

3. display user's name on home page

here is my code, very simple..

login.jsx

import React, {useState} from "react" import {userLogin} from "../../services/userService"

function Login(){ const [formData, setFormData] = useState({ email:"", password:"", tenantId:"1234", });

const onFormFieldChange= event =>{ console.log("onChange", { syntheticEvent: event });

const target = event.target;

const newUserValue = target.value;

const nameOfField = target.name; setFormData ((prevState)=>{

const newUserObject ={...prevState}; newUserObject[nameOfField] = newUserValue; console.log("new user value", newUserValue) return newUserObject; }); };

const onClickLogin=(e)=>{ e.preventDefault(); userLogin(formData) .then(onUserLoginSuccess) .catch(onUserLoginError);

}

const onUserLoginSuccess=()=> console.log( "You have logged in successfully!","Login success")

const onUserLoginError=()=>console.log("Please check your email or password","Login Failed")

return (

)

} export default Login;

Navigation.jsx

import React from 'react'

import {Link} from 'react-router-dom'

function SiteNavigation (){

return

Login

}

export default SiteNavigation;

Home.jsx

import React from 'react'

import {getCurrentUser} from.... (whatever ajax.js location)

function Home(){

return (

Hello _____________________<- here, I would like to put current logged in user's name

)}

export default Home.

ajax.js

import axios from "axios"

const endpoint = "xxxxxxxx/api/users"

const userLogin =(payload)=>{

const config ={

method: "post",

url: endpoint +"/login" data: payload,

crossdomain: true}

return axios (config)

}

const currentUser =(payload)=>{

const config ={

method: "GET",

url: endpoint +"/current" data: payload,

crossdomain: true}

return axios (config)

}

export default {userLogin, getCurrentuser}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

Students also viewed these Databases questions

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago