LoginCtrl.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React from 'react';
  2. import config from 'app/core/config';
  3. import { updateLocation } from 'app/core/actions';
  4. import { connect } from 'react-redux';
  5. import { StoreState } from 'app/types';
  6. import { PureComponent } from 'react';
  7. import { getBackendSrv } from '@grafana/runtime';
  8. import { hot } from 'react-hot-loader';
  9. import appEvents from 'app/core/app_events';
  10. const isOauthEnabled = () => {
  11. return !!config.oauth && Object.keys(config.oauth).length > 0;
  12. };
  13. export interface FormModel {
  14. user: string;
  15. password: string;
  16. email: string;
  17. }
  18. interface Props {
  19. routeParams?: any;
  20. updateLocation?: typeof updateLocation;
  21. children: (props: {
  22. isLoggingIn: boolean;
  23. changePassword: (pw: string) => void;
  24. isChangingPassword: boolean;
  25. skipPasswordChange: Function;
  26. login: (data: FormModel) => void;
  27. disableLoginForm: boolean;
  28. ldapEnabled: boolean;
  29. authProxyEnabled: boolean;
  30. disableUserSignUp: boolean;
  31. isOauthEnabled: boolean;
  32. loginHint: string;
  33. passwordHint: string;
  34. }) => JSX.Element;
  35. }
  36. interface State {
  37. isLoggingIn: boolean;
  38. isChangingPassword: boolean;
  39. }
  40. export class LoginCtrl extends PureComponent<Props, State> {
  41. result: any = {};
  42. constructor(props: Props) {
  43. super(props);
  44. this.state = {
  45. isLoggingIn: false,
  46. isChangingPassword: false,
  47. };
  48. if (config.loginError) {
  49. appEvents.emit('alert-warning', ['Login Failed', config.loginError]);
  50. }
  51. }
  52. changePassword = (password: string) => {
  53. const pw = {
  54. newPassword: password,
  55. confirmNew: password,
  56. oldPassword: 'admin',
  57. };
  58. getBackendSrv()
  59. .put('/api/user/password', pw)
  60. .then(() => {
  61. this.toGrafana();
  62. })
  63. .catch((err: any) => console.log(err));
  64. };
  65. login = (formModel: FormModel) => {
  66. this.setState({
  67. isLoggingIn: true,
  68. });
  69. getBackendSrv()
  70. .post('/login', formModel)
  71. .then((result: any) => {
  72. this.result = result;
  73. if (formModel.password !== 'admin' || config.ldapEnabled || config.authProxyEnabled) {
  74. this.toGrafana();
  75. return;
  76. } else {
  77. this.changeView();
  78. }
  79. })
  80. .catch(() => {
  81. this.setState({
  82. isLoggingIn: false,
  83. });
  84. });
  85. };
  86. changeView = () => {
  87. this.setState({
  88. isChangingPassword: true,
  89. });
  90. };
  91. toGrafana = () => {
  92. const params = this.props.routeParams;
  93. // Use window.location.href to force page reload
  94. if (params.redirect && params.redirect[0] === '/') {
  95. window.location.href = config.appSubUrl + params.redirect;
  96. } else if (this.result.redirectUrl) {
  97. window.location.href = config.appSubUrl + this.result.redirectUrl;
  98. } else {
  99. window.location.href = config.appSubUrl + '/';
  100. }
  101. };
  102. render() {
  103. const { children } = this.props;
  104. const { isLoggingIn, isChangingPassword } = this.state;
  105. const { login, toGrafana, changePassword } = this;
  106. const { loginHint, passwordHint, disableLoginForm, ldapEnabled, authProxyEnabled, disableUserSignUp } = config;
  107. return (
  108. <>
  109. {children({
  110. isOauthEnabled: isOauthEnabled(),
  111. loginHint,
  112. passwordHint,
  113. disableLoginForm,
  114. ldapEnabled,
  115. authProxyEnabled,
  116. disableUserSignUp,
  117. login,
  118. isLoggingIn,
  119. changePassword,
  120. skipPasswordChange: toGrafana,
  121. isChangingPassword,
  122. })}
  123. </>
  124. );
  125. }
  126. }
  127. export const mapStateToProps = (state: StoreState) => ({
  128. routeParams: state.location.routeParams,
  129. });
  130. const mapDispatchToProps = { updateLocation };
  131. export default hot(module)(
  132. connect(
  133. mapStateToProps,
  134. mapDispatchToProps
  135. )(LoginCtrl)
  136. );