My 'Login' endpoint works fine, and I set the 'propertyName' to 'access_token' as that is where the value for my token lives. I see 'Vuex' update my 'LoggedIn' status to true and I can also see the Token Response in the 'Network' tab of Chrome.
However, I'm really struggling to understand how the 'User' endpoint works.
The example is given:
auth: {
strategies: {
login: {
scheme: 'local',
token: {
property: 'access_token',
required: true,
type: 'Bearer',
},
refreshToken: {
property: 'refresh_token',
data: 'refresh_token',
maxAge: 60 * 60 * 24 * 30
},
user: {
property: false,
autoFetch: true,
},
autoLogout: true,
endpoints: {
login: { url: '/registration/login', method: 'post', propertyName: 'access_token'},
user: {
url: '/registration/requests/1',
method: 'get',
propertyName: false
},
logout: {
url: '/registration/logout', method: 'post'
},
},
},
},
redirect: {
login: '/news',
},
},
The above is pretty much identical to mine, how does the 'User' endpoint, know which user is logged in?
I am using a third-party system for my authentication as I'm integrating an application into the third-party system. Their 'User' endpoint for REST requires an 'ID' or 'UserName' to return details about a particular user.
My 'Login' response contains 'UserName' which I could use to call the subsequent User endpoint (If I knew how).
Does anyone know how the User endpoint works? Essentially I need to call it something like this:
user: { url: '/users/${userId}', method: 'get', }
Related
What I'm doing is a mobile application using Nuxt and Capacitor.
Since the Nuxt Auth module doesn't support in app dialogs for oAuth authentications, what I'm trying to do is to mix the capacitor-google-auth with the Auth module.
I perform correctly the oAuth login with Google and I can get the credentials.
Then I force the Auth Module to set its strategy to Google in this way (following their documentation)
// method 1
this.$auth.$storage.setUniversal('strategy', 'google')
this.$auth.strategy.token.set(token)
const user = await this.$auth.fetchUser()
await this.$auth.setUser(user)
// method 2
this.$auth.$storage.setUniversal('strategy', 'google')
await this.$auth.setUserToken(token)
I can correctly see the calls to my API in both ways, the user get fetched correctly the first time.
Unfortunately the token in both the cookie and local storage is not set, or better, it is set for a millisecond, then it get cleared...
Once completed the process the try catch get an error and it is undefined.
Do you know how can I let it work? Do you need more information?
Thanks
EDIT: Adding auth configuration:
auth: {
resetOnError: true,
localStorage: false,
cookie: {
prefix,
options: {
maxAge,
secure: true,
domain,
},
},
redirect: {
login: '/',
logout: '/',
home: '/home',
},
strategies: {
local: {
endpoints: {
login: {
url: '/auth/local',
method: 'POST',
propertyName: 'token',
},
logout: { url: '/auth/logout', method: 'POST' },
user: { url: '/me', method: 'GET', propertyName: false },
},
tokenRequired: true,
tokenType: 'Bearer',
},
},
},
I realize that you authorize your user with google OAuth2 and receive a token from Google, and you want to store that. I had the same issue; after registering a user, I wanted to keep the user logged in. So in my case, I receive the token from register API and then use nuxt-auth to set the tokens.
Here is my code after the user registers:
try {
this.$bvToast.hide('register-error-toast')
let response = await this.$axios.post('/api/auth/register/', this.user_data)
// these two methods are what you can try
this.$auth.setUser(response.data.user)
this.$auth.setUserToken(response.data.token)
} catch (error) {
}
And this is my auth config in the nuxt.config.js file:
auth: {
redirect: {
login: '/auth/login',
logout: '/',
home: '/'
},
cookie: {
options: {
maxAge: 12 * 24 * 60 * 60
}
},
strategies: {
local: {
autoFetchUser: false,
token: {
property: 'token',
type: 'Bearer'
},
user: {
property: 'user',
autoFetch: false
},
endpoints: {
login: {
url: process.env.BASE_URL + '/api/auth/login/',
method: 'post'
},
user: false,
}
}
}
}
I shared my whole config with you, and it may have some excessive data; you can change the config base on your requirements.
So we implemented a google signed in for our app. The signed in works, once the user is signed in, the authentication is done in the back end. The problem is that when the user logs in and is granted access to the pages in nuxt, if the user refreshes the window, it gets logged out.
This is my nuxt.config
auth: {
strategies: {
local: {
endpoints: {
login: {url: 'auth/login', method: 'post', propertyName: 'token'},
user: { url: 'user', method: 'get', propertyName: 'data'},
logout: { method: 'get', url: 'logout'}
}
}
},
redirect:{
logout: '/signin'
}
},
router: {
middleware: ['auth']
},
I am setting the user with nuxt auth library.
here is what I have:
export default {
auth: false,
data() {
console.log('token',this.$route.query.token)
return {
token: this.$route.query.token ? this.$route.query.token : null
}
},
mounted() {
this.$auth.setUserToken('Bearer ' + this.token).then((res) => {
console.log(res.data.data)
this.$auth.setUser(res.data.data);
return this.$router.push('/');
})
.catch( (e) => {
this.$auth.logout();
return this.$router.push(`/auth/login?error=1`);
});
}
}
any ideas how I can keep the user logged in to true when the winder reloads?
You have to save the token somewhere and restore it on page reload,
you could use their built-in $storage methods https://auth.nuxtjs.org/api/storage
i used Nuxt.js
and i used auth Nuxt.js .
after login to dashboard page by refresh goto auth and return to dashboard page.
by routing all is ok but by refresh not correct!
auth: {
cookie: {
prefix: 'auth.',
options: {
maxAge: '3600'
}
},
redirect: {
login: '/auth/login',
logout: '/auth/login',
callback: false,
home: false
},
strategies: {
local: {
login: { url: '/api/auth/login', method: 'post', propertyName: 'token' },
logout: { url: '/api/auth/logout', method: 'post' },
user: { url: '/api/auth/user', method: 'get', propertyName: 'user' }
},
// token_type: '',
}
},
and in post/_slug.vue
by refresh return 404 error !
mode: universal
ssr : true
Iam trying to use nuxt-auth module with axios and it gives me "Cannot read property 'data' of undefined" ...and login headers gives strange path
"Request URL: http://aqar.abdullah.link/api/api/auth/login",any help please???
Nuxt.config.js
axios: {
baseURL:'http://aqar.abdullah.link/api',
auth: {
strategies: {
local: {
endpoints: {
login: { url: '/office/login', method: 'post', propertyName: 'data.token' },
user: { url: '/auth/me', method: 'post', propertyName: false },
logout: false
}
}
}
}
}
Login Method:
methods:{
async Login(){
try{
await this.$auth.loginWith('local', {
data:{
email: this.email,
password: this.password
}
})
this.$router.push('/')
}catch(e){
this.error = e.response.data
}
},
},
Api response on postman:
login page path in folders
I'm not sure, I used the exact same configurations and it worked for me.
But your nuxt.config.js file has a missing closing }, for the axios object.
it should look like this:
axios: {
baseURL: "http://aqar.abdullah.link/api"
},
auth: {
strategies: {
local: {
endpoints: {
login: {
url: "/office/login",
method: "post",
propertyName: "data.token"
},
user: { url: "/auth/me", method: "post", propertyName: false },
logout: false
}
}
}
},
also make sure you have these modules in you nuxt.config.js as well:
modules: [
"#nuxt/http",
"#nuxtjs/auth",
"#nuxtjs/axios",
],
How to pass access_token to the user endpoint in nuxt/auth package while you got the access_token as directly without any username and password, I don't want to call login before the user endpoint because of access_token is already and I just want to call user enpoint, finally how do i pass access_token and call the user endpoint directly?
Thanks so much for any help!
// Auth module
auth: {
plugins: [
{ src: '~/plugins/auth' }
],
redirect: {
login: '/login',
logout: '/',
home: false
},
strategies: {
local: {
endpoints: {
login: { url: 'account/token', propertyName: 'result.access_token' },
logout: { url: 'account/logout' },
user: { url: 'account/self', propertyName: 'result' }
}
}
}
}
To set token you could use this.$auth.setToken(...). To call user endpoint you could use this.$auth.fetchUser().
Docs