-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next): add getAccessToken and getOrganizationToken for pages rou…
…ter (#782)
- Loading branch information
Showing
7 changed files
with
98 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@logto/next': minor | ||
--- | ||
|
||
add getAccessToken and getOrganizationToken methods for pages router | ||
|
||
You can now use `getAccessToken(request, response, 'resource-indicator')` to get access token directly in your pages router. And `getOrganizationToken` is also available to get organization token. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { logtoClient } from '../../libraries/logto'; | ||
|
||
export default logtoClient.withLogtoApiRoute(async (request, response) => { | ||
if (!request.user.isAuthenticated) { | ||
response.status(401).json({ message: 'Unauthorized' }); | ||
|
||
return; | ||
} | ||
|
||
const organizationToken = await logtoClient.getOrganizationToken( | ||
request, | ||
response, | ||
'organization_id' | ||
); | ||
|
||
// Do something with the organization token | ||
console.log(organizationToken); | ||
|
||
response.end(); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
import { logtoClient } from '../../libraries/logto'; | ||
|
||
export default logtoClient.withLogtoApiRoute( | ||
(request, response) => { | ||
if (!request.user.isAuthenticated) { | ||
response.status(401).json({ message: 'Unauthorized' }); | ||
export default logtoClient.withLogtoApiRoute(async (request, response) => { | ||
if (!request.user.isAuthenticated) { | ||
response.status(401).json({ message: 'Unauthorized' }); | ||
|
||
return; | ||
} | ||
return; | ||
} | ||
|
||
// Get an access token with the target resource | ||
// console.log(request.user.accessToken); | ||
// Get an access token with the target resource | ||
const accessToken = await logtoClient.getAccessToken(request, response, 'resource-indicator'); | ||
// Do something with the access token | ||
console.log(accessToken); | ||
|
||
response.json({ | ||
data: 'this_is_protected_resource', | ||
}); | ||
}, | ||
{ | ||
// (Optional) getAccessToken: true, | ||
// (Optional) resource: 'https://the-resource.domain/' | ||
} | ||
); | ||
response.json({ | ||
data: 'this_is_protected_resource', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters