Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Show terms modal when inviting by email #3271

Merged
merged 3 commits into from
Aug 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/IdentityAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { SERVICE_TYPES } from 'matrix-js-sdk';

import MatrixClientPeg from './MatrixClientPeg';
import { Service, startTermsFlow, TermsNotSignedError } from './Terms';

export default class IdentityAuthClient {
constructor() {
Expand All @@ -40,24 +43,46 @@ export default class IdentityAuthClient {

if (!token) {
token = await this.registerForToken();
this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token);
if (token) {
this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token);
}
return token;
}

try {
await this._checkToken(token);
} catch (e) {
if (e instanceof TermsNotSignedError) {
// Retrying won't help this
throw e;
}
// Retry in case token expired
token = await this.registerForToken();
this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token);
if (token) {
this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token);
}
}

return token;
}

async _checkToken(token) {
await MatrixClientPeg.get().getIdentityAccount(token);
try {
await MatrixClientPeg.get().getIdentityAccount(token);
} catch (e) {
if (e.errcode === "M_TERMS_NOT_SIGNED") {
console.log("Identity Server requires new terms to be agreed to");
await startTermsFlow([new Service(
SERVICE_TYPES.IS,
MatrixClientPeg.get().idBaseUrl,
token,
)]);
return;
}
throw e;
}

// We should ensure the token in `localStorage` is cleared
// appropriately. We already clear storage on sign out, but we'll need
Expand All @@ -73,16 +98,16 @@ export default class IdentityAuthClient {
await MatrixClientPeg.get().registerWithIdentityServer(hsOpenIdToken);
await this._checkToken(identityAccessToken);
return identityAccessToken;
} catch (err) {
if (err.cors === "rejected" || err.httpStatus === 404) {
} catch (e) {
if (e.cors === "rejected" || e.httpStatus === 404) {
// Assume IS only supports deprecated v1 API for now
// TODO: Remove this path once v2 is only supported version
// See https://github.com/vector-im/riot-web/issues/10443
console.warn("IS doesn't support v2 auth");
this.authEnabled = false;
return;
}
console.error(err);
throw e;
}
}
}