Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
e0c331e4
Commit
e0c331e4
authored
Jan 04, 2023
by
Lyza Danger Gardner
Committed by
Lyza Gardner
Jan 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert auth service to TS
parent
2274dd7a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
37 deletions
+34
-37
auth.ts
src/sidebar/services/auth.ts
+34
-37
No files found.
src/sidebar/services/auth.
j
s
→
src/sidebar/services/auth.
t
s
View file @
e0c331e4
import
{
TinyEmitter
}
from
'tiny-emitter'
;
import
{
serviceConfig
}
from
'../config/service-config'
;
import
type
{
APIRoutesService
}
from
'./api-routes'
;
import
type
{
LocalStorageService
}
from
'./local-storage'
;
import
type
{
SidebarSettings
}
from
'../../types/config'
;
import
type
{
ToastMessengerService
}
from
'./toast-messenger'
;
import
{
OAuthClient
}
from
'../util/oauth-client'
;
import
type
{
TokenInfo
}
from
'../util/oauth-client'
;
import
{
resolve
}
from
'../util/url'
;
/**
* @typedef {import('../util/oauth-client').TokenInfo} TokenInfo
*
* @typedef RefreshOptions
* @prop {boolean} persist - True if access tokens should be persisted for
* use in future sessions.
*/
type
RefreshOptions
=
{
/**
* True if access tokens should be persisted for use in future sessions.
*/
persist
:
boolean
;
};
/**
* Authorization service.
...
...
@@ -28,33 +32,32 @@ import { resolve } from '../util/url';
* @inject
*/
export
class
AuthService
extends
TinyEmitter
{
/**
* @param {Window} $window
* @param {import('./api-routes').APIRoutesService} apiRoutes
* @param {import('./local-storage').LocalStorageService} localStorage
* @param {import('./toast-messenger').ToastMessengerService} toastMessenger
* @param {import('../../types/config').SidebarSettings} settings
*/
constructor
(
$window
,
apiRoutes
,
localStorage
,
settings
,
toastMessenger
)
{
public
login
:
()
=>
void
;
public
logout
:
()
=>
void
;
public
getAccessToken
:
()
=>
void
;
constructor
(
$window
:
Window
,
apiRoutes
:
APIRoutesService
,
localStorage
:
LocalStorageService
,
settings
:
SidebarSettings
,
toastMessenger
:
ToastMessengerService
)
{
super
();
/**
* Authorization code from auth popup window.
* @type {string|null}
*/
let
authCode
;
let
authCode
:
string
|
null
;
/**
* Token info retrieved via `POST /api/token` endpoint.
*
* Resolves to `null` if the user is not logged in.
*
* @type {Promise<TokenInfo|null>|null}
*/
let
tokenInfoPromise
;
let
tokenInfoPromise
:
Promise
<
TokenInfo
|
null
>
|
null
;
/** @type {OAuthClient} */
let
client
;
let
client
:
OAuthClient
;
/**
* Absolute URL of the `/api/token` endpoint.
...
...
@@ -63,10 +66,8 @@ export class AuthService extends TinyEmitter {
/**
* Show an error message telling the user that the access token has expired.
*
* @param {string} message
*/
function
showAccessTokenExpiredErrorMessage
(
message
)
{
function
showAccessTokenExpiredErrorMessage
(
message
:
string
)
{
toastMessenger
.
error
(
`Hypothesis login lost:
${
message
}
`
,
{
autoDismiss
:
false
,
});
...
...
@@ -114,10 +115,8 @@ export class AuthService extends TinyEmitter {
/**
* Persist access & refresh tokens for future use.
*
* @param {TokenInfo} token
*/
function
saveToken
(
token
)
{
function
saveToken
(
token
:
TokenInfo
)
{
localStorage
.
setObject
(
storageKey
(),
token
);
}
...
...
@@ -152,12 +151,11 @@ export class AuthService extends TinyEmitter {
/**
* Exchange a refresh token for a new access token and refresh token pair.
*
* @param {string} refreshToken
* @param {RefreshOptions} options
* @return {Promise<TokenInfo|null>} Promise for the new access token
*/
const
refreshAccessToken
=
async
(
refreshToken
,
options
)
=>
{
const
refreshAccessToken
=
async
(
refreshToken
:
string
,
options
:
RefreshOptions
):
Promise
<
TokenInfo
|
null
>
=>
{
const
client
=
await
oauthClient
();
let
token
;
...
...
@@ -186,9 +184,8 @@ export class AuthService extends TinyEmitter {
* Exchange authorization code retrieved from login popup for a new
* access token.
*
* @param {string} code
*/
const
exchangeAuthCodeForToken
=
async
code
=>
{
const
exchangeAuthCodeForToken
=
async
(
code
:
string
)
=>
{
const
client
=
await
oauthClient
();
const
tokenInfo
=
await
client
.
exchangeAuthCode
(
code
);
saveToken
(
tokenInfo
);
...
...
@@ -207,9 +204,9 @@ export class AuthService extends TinyEmitter {
/**
* Retrieve an access token for the API.
*
* @return
{Promise<string|null>} The API access token
or `null` if not logged in.
* @return
The API access token string
or `null` if not logged in.
*/
const
getAccessToken
=
async
()
=>
{
const
getAccessToken
=
async
()
:
Promise
<
string
|
null
>
=>
{
// Determine how to get an access token, depending on the login method being used.
if
(
!
tokenInfoPromise
)
{
const
grantToken
=
getGrantToken
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment