-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathsmart-profile.tsx
137 lines (114 loc) · 3.71 KB
/
smart-profile.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {PureComponent} from 'react';
import PropTypes from 'prop-types';
import Auth, {
USER_CHANGED_EVENT,
LOGOUT_POSTPONED_EVENT,
USER_CHANGE_POSTPONED_EVENT, AuthUser
} from '../auth/auth';
import alertService from '../alert-service/alert-service';
import {Size} from '../avatar/avatar';
import Profile, {ProfileAttrs} from './profile';
const CERTIFICATE_MISMATCH_HEADER = 'x-client-certificate-token-mismatch';
export interface SmartProfileProps extends ProfileAttrs {
auth: Auth
}
interface SmartProfileState {
user: AuthUser | null | undefined | void,
size: Size,
isLogoutPostponed: boolean,
isUserChangePostponed: boolean
loading?: boolean
}
export default class SmartProfile extends PureComponent<SmartProfileProps, SmartProfileState> {
static propTypes = {
auth: PropTypes.instanceOf(Auth).isRequired,
className: PropTypes.string,
translations: PropTypes.object,
profileUrl: PropTypes.string,
size: Profile.propTypes.size,
round: Profile.propTypes.round
};
state: SmartProfileState = {
user: null,
size: Profile.defaultProps.size,
isLogoutPostponed: false,
isUserChangePostponed: false
};
componentDidMount() {
this.requestUser();
}
static Size = Profile.Size;
login = async () => {
this.setState({loading: true});
try {
await this.props.auth.login();
} catch (err) {
// eslint-disable-next-line no-console
console.debug('Profile login errored', err);
} finally {
this.setState({loading: false});
}
};
logout = () => this.props.auth.logout();
switchUser = () => this.props.auth.switchUser();
onRevertPostponement = () => {
if (this.state.isLogoutPostponed) {
this.props.auth.login();
}
if (this.state.isUserChangePostponed) {
this.props.auth.updateUser();
}
};
async requestUser() {
try {
const {auth} = this.props;
const user = await auth.requestUser();
this.checkUserCertificateMismatch(user);
this.setState({user});
auth.addListener(USER_CHANGED_EVENT, newUser => {
this.setState({
user: newUser,
isLogoutPostponed: false,
isUserChangePostponed: false
});
});
auth.addListener(LOGOUT_POSTPONED_EVENT, () => {
this.setState({isLogoutPostponed: true});
});
auth.addListener(USER_CHANGE_POSTPONED_EVENT, () => {
this.setState({isUserChangePostponed: true});
});
} catch (e) {
// noop
}
}
checkUserCertificateMismatch(user: AuthUser) {
const {auth, translations} = this.props;
const userMeta = auth.http.getMetaForResponse(user);
if (userMeta?.headers?.has(CERTIFICATE_MISMATCH_HEADER)) {
const message = translations?.certificateMismatch || `You are authenticated as ${user.login || user.name}. To authenticate with the client certificate for your account, log out, then click the "Log in with certificate" option on the login page.`;
alertService.warning(message, 0);
}
}
render() {
const {user, loading, isLogoutPostponed, isUserChangePostponed} = this.state;
const {auth, profileUrl, ...props} = this.props;
const url = profileUrl || (user ? `${auth.config.serverUri}users/${user.id}` : '');
return (
<Profile
onLogin={this.login}
onLogout={this.logout}
onSwitchUser={this.switchUser}
loading={loading}
user={user}
profileUrl={url}
showApplyChangedUser={isUserChangePostponed}
showLogIn={isLogoutPostponed}
showLogOut={!isLogoutPostponed}
showSwitchUser={auth._canShowDialogs() && !isLogoutPostponed && !isUserChangePostponed}
onRevertPostponement={this.onRevertPostponement}
{...props}
/>
);
}
}