Is there a way to retrieve the user token in the next gen code? Knack.getUserToken() does not seem to exist anymore.
Hi @Shan, currently there is no way to do so, but myself and other users have requested this and some other Classic functions to be made available to NextGen.
@Shan @StephenChapman The ability to get the user token is currently being QA’d, hopefully we can get it released in the next week or so.
Has this been completed yet?
Yes, this functionality was released a few weeks ago - 🚀 Week 43 / October 21: Next-Gen Custom JS & Security Enhancements - NEW | Knack
I tried the sample code from the documentation and the browser is returning no user is currently logged in. This is obviously not true because I can browse all of the protected pages with my account. Is there anything special that needs to be done to get this to work?
Knack.ready().then(async () => {
// Get current user information
const user = await Knack.getUser();
if (user) {
console.log(‘Current user:’, user);
console.log(‘User email:’, user.email);
console.log(‘User name:’, user.name);
} else {
console.log(‘No user is currently logged in’);
}
});
@Kara I can confirm the same with the same code above if calling immediately. It works the odd time, but the majority of times it doesn’t return a value, even when logged in.
@StephenChapman @IAmAKnacker Thanks for letting me know, I’ve asked the team to look into this.
There may have been a syntax error in the documentation (Left Single Quotation Mark (U+2018) and a Right Single Quotation Mark (U+2019), it’s been updated. Two engineers have tested the code and it’s worked correctly each time - after the correction, does it work consistently for you?
Knack.ready().then(async () => {
// Get current user information
const user = await Knack.getUser();
if (user) {
console.log('Current user:', user);
console.log('User email:', user.email);
console.log('User name:', user.name);
} else {
console.log('No user is currently logged in');
}
// Check if the current user has a specific profile/role key
try {
const hasProfile = await Knack.hasProfileKey('profile_1'); // e.g., 'profile_1'
if (typeof hasProfile === 'boolean') {
console.log('Has profile_1?', hasProfile);
}
} catch (e) {
// Swallow if not supported
}
});
interface SchemaUser {
id: string;
email: string;
name: string;
status: string;
profileKeys: KnackObjectProfileKey[];
profileObjects: string[];
}
@Kara, still no luck. I made sure to correct the quotation marks (code below), but it works at least once and then never again after refreshing.
I added a 250ms delay, and it worked okay. I wonder if the user details are not immediately available on Knack.ready?
Not that this is super important, as I would only ever collect user details on scene render, as this would account for users logging in/out in the same session.
Knack.ready().then(async () => {
// Get current user information
const user = await Knack.getUser();
if (user) {
console.log('Current user:', user);
console.log('User email:', user.email);
console.log('User name:', user.name);
} else {
console.log('No user is currently logged in');
}
});
This is working on a next gen app for me:
Knack.ready().then(async () => {
Knack.on('page:render', async ({ pageKey }) => {
try {
const user = await Knack.getUser();
if (user) {
console.log('User email:', user.email);
console.log('User name:', user.name);
console.log('User id:', user.id);
// do something here with user object
} else {
console.log('No user is logged in');
}
} catch (error) {
console.error('Error identifying user: ', error);
}
});
});
Looks like that worked.
“Knack.on(‘page:render’, async ({ pageKey }) => {”
Seems to have fixed it. Thank’s Jake!
@Kara - How exactly do I get the session token. I looked at all of the values and can not find anything that indicates it is a session token. I see all of the fields returned in the request, but not the session token.
@IAmAKnacker, user.token is what you’re after:
const user = await Knack.getUser()
const token = user.token;
Here’s the full object:
@Kara - am I not getting the token returned. Is there something that you guys need to do to fix this?
Not 100% sure if this will work (not sure how you are using the token?) but there is a value you can access in the user object on front end, user.values.password , which looks like it might be a token
I don’t have any front end code which calls a knack api to test if this is actually functional, but try it out?
It also looks like next gen uses Local Storage to keep track of the refreshToken, but not sure if it will allow api calls with that. A knack internal engineer would know; I am not one ![]()
I also do not get anything back for user.token on the front end, not sure if Stephen has special powers somehow or was maybe accidentally testing on classic apps?
That’s just an encrypted version of the password, not the session token. I need the token to authenticate to the view-based APIs
@IAmAKnacker, I suggest you raise a ticket with support@knack.com if you still can’t see that user token, as it should be appearing in that getUser() object.


