ga-wdi-boston / game-project

Other
7 stars 102 forks source link

difficulty with sign out feature #823

Closed ryanwk closed 7 years ago

ryanwk commented 7 years ago

I'm having trouble with my sign-out button. I've got everything set up according to the api-auth-token documentation, however, I think I'm having difficulty with understanding how user data is stored.

here is my code:


index.js
  $('#sign-out').on('click', function (e) {
    // const data = getFormFields(this)
    e.preventDefault()
    api.signOut()
  })
})
api.js
const signOut = () => {
  return $.ajax({
    url: config.apiOrigin + '/sign-out/:id' + store.user,
    method: 'DELETE',
    headers: {
      Authorization: 'Token token=' + store.user.token
    }
  })

i think the problems stems from here:

    url: config.apiOrigin + '/sign-out/:id' + store.user,
    method: 'DELETE',
    headers: {
      Authorization: 'Token token=' + store.user.token

this is my error: api.js:42 Uncaught TypeError: Cannot read property 'token' of undefined

this is what I've tried: -checked everything to see if it is being exported and required properly -read the api-token-auth documentation -reread game-project-api documentation -modifiying this '/sign-out/:id'

const signOut = () => {
  return $.ajax({
    url: config.apiOrigin + '/sign-out/:id' + store.user,
    method: 'DELETE',
    headers: {
      Authorization: 'Token token=' + store.user.token
    }
  })

this is an almost identical issue and I'm trying to understand how it was corrected but some of her code is throwing me off: https://github.com/ga-wdi-boston/game-project/issues/722

Jcornmanhomonoff commented 7 years ago

You have the right idea. I think you're trying to pass through the user id twice in the URL though. Also, if you console.log(store.user) what do you get back as a response?

ryanwk commented 7 years ago

i removed 'id' from the url:

 url: config.apiOrigin + '/sign-out/:id' + store.user,

I added a console log but it displays 'undefined'

jordanallain commented 7 years ago

when API documentation looks like this

/sign-out/:id

that typically is telling us that :id will be replaced by the actual id number when you make the request.

so you'd want the url to look more like /sign-out/4 where 4 is the id number of the user that you are trying to sign out.

ryanwk commented 7 years ago

I can see how that is useful, but I'm not entirely sure what to do with it. Should I add something to my sign-in function that gives the user an ID?

let user = 0
const success = (data) => {
  // handle success
  console.log('user has signed out: ' + data)
  user++
}
jordanallain commented 7 years ago

the user will have an id assigned to it when they sign up by the back end. what you want to do is store the user when they successfully sign in.

want to take a look at it together?

ryanwk commented 7 years ago

was not storing user after sign in because I didn't have the ID I added something to my sign in success function and api.js signOut function that stored the user ID and added then something to update store again to remove the ID upon signing out