20200516

Bindung·2020년 5월 16일
0

Today I Learned

목록 보기
10/16
post-thumbnail

스터디 내용

  • Post로 로그인 토큰을 요청
  • 로그인 토큰이 성공하면 유저정보를 요청하여서 로그인한다.

스터디 후기

  • 어제와 별다를것 없는 내용이였다.
  • axios의 Post
//post 2번째 인자는 BODY의 파라메터를 가져올 수 있음
.post("https://reqres.in/api/login", loginObj)
  • axios의 get
// GET 2번째 인자는 config가 옴
.get("https://reqres.in/api/users/2", config) 
  • 로그인 부분 action소스
login({ commit }, loginObj) {
      axios
        .post("https://reqres.in/api/login", loginObj) //포스트 2번째 인자는 BODY의 파라메터를 가져올 수 있음
        .then(res => {
          // 성공 시 토큰 (실제로는 유저아이디 받아옴)
          // 해더에 포함하여 유저정보 요청
          let token = res.data.token
          let config = {
            // 헤더값을 설정할 수 있음
            headers: {
              "access-token": token
            }
          }
          axios
            .get("https://reqres.in/api/users/2", config) // GET방식은 config가 옴
            .then(response => {
              let userInfo = {
                id: response.data.data.id,
                first_name: response.data.data.first_name,
                last_name: response.data.data.last_name,
                avatar: response.data.data.avatar
              }
              commit("loginSuccess", userInfo)
            })
            .catch(error => {
              alert("이메일과 비밀번호를 확인하세요.")
              console.log(error)
            })
        })
        .catch(err => {
          // handle error
          console.log(err)
        })
    }
profile
포기하지말고 천천히...

0개의 댓글