11window . matrixLogin = {
22 endpoint : location . origin + "/_matrix/client/r0/login" ,
33 serverAcceptsPassword : false ,
4- serverAcceptsCas : false ,
54 serverAcceptsSso : false ,
65} ;
76
7+ var title_pre_auth = "Log in with one of the following methods" ;
8+ var title_post_auth = "Logging in..." ;
9+
810var submitPassword = function ( user , pwd ) {
911 console . log ( "Logging in with password..." ) ;
12+ set_title ( title_post_auth ) ;
1013 var data = {
1114 type : "m.login.password" ,
1215 user : user ,
1316 password : pwd ,
1417 } ;
1518 $ . post ( matrixLogin . endpoint , JSON . stringify ( data ) , function ( response ) {
16- show_login ( ) ;
1719 matrixLogin . onLogin ( response ) ;
1820 } ) . error ( errorFunc ) ;
1921} ;
2022
2123var submitToken = function ( loginToken ) {
2224 console . log ( "Logging in with login token..." ) ;
25+ set_title ( title_post_auth ) ;
2326 var data = {
2427 type : "m.login.token" ,
2528 token : loginToken
2629 } ;
2730 $ . post ( matrixLogin . endpoint , JSON . stringify ( data ) , function ( response ) {
28- show_login ( ) ;
2931 matrixLogin . onLogin ( response ) ;
3032 } ) . error ( errorFunc ) ;
3133} ;
3234
3335var errorFunc = function ( err ) {
34- show_login ( ) ;
36+ // We want to show the error to the user rather than redirecting immediately to the
37+ // SSO portal (if SSO is the only login option), so we inhibit the redirect.
38+ show_login ( true ) ;
3539
3640 if ( err . responseJSON && err . responseJSON . error ) {
3741 setFeedbackString ( err . responseJSON . error + " (" + err . responseJSON . errcode + ")" ) ;
@@ -45,26 +49,33 @@ var setFeedbackString = function(text) {
4549 $ ( "#feedback" ) . text ( text ) ;
4650} ;
4751
48- var show_login = function ( ) {
49- $ ( "#loading" ) . hide ( ) ;
50-
52+ var show_login = function ( inhibit_redirect ) {
5153 var this_page = window . location . origin + window . location . pathname ;
5254 $ ( "#sso_redirect_url" ) . val ( this_page ) ;
5355
54- if ( matrixLogin . serverAcceptsPassword ) {
55- $ ( "#password_flow" ) . show ( ) ;
56+ // If inhibit_redirect is false, and SSO is the only supported login method, we can
57+ // redirect straight to the SSO page
58+ if ( matrixLogin . serverAcceptsSso ) {
59+ if ( ! inhibit_redirect && ! matrixLogin . serverAcceptsPassword ) {
60+ $ ( "#sso_form" ) . submit ( ) ;
61+ return ;
62+ }
63+
64+ // Otherwise, show the SSO form
65+ $ ( "#sso_form" ) . show ( ) ;
5666 }
5767
58- if ( matrixLogin . serverAcceptsSso ) {
59- $ ( "#sso_flow" ) . show ( ) ;
60- } else if ( matrixLogin . serverAcceptsCas ) {
61- $ ( "#sso_form" ) . attr ( "action" , "/_matrix/client/r0/login/cas/redirect" ) ;
62- $ ( "#sso_flow" ) . show ( ) ;
68+ if ( matrixLogin . serverAcceptsPassword ) {
69+ $ ( "#password_flow" ) . show ( ) ;
6370 }
6471
65- if ( ! matrixLogin . serverAcceptsPassword && ! matrixLogin . serverAcceptsCas && ! matrixLogin . serverAcceptsSso ) {
72+ if ( ! matrixLogin . serverAcceptsPassword && ! matrixLogin . serverAcceptsSso ) {
6673 $ ( "#no_login_types" ) . show ( ) ;
6774 }
75+
76+ set_title ( title_pre_auth ) ;
77+
78+ $ ( "#loading" ) . hide ( ) ;
6879} ;
6980
7081var show_spinner = function ( ) {
@@ -74,17 +85,15 @@ var show_spinner = function() {
7485 $ ( "#loading" ) . show ( ) ;
7586} ;
7687
88+ var set_title = function ( title ) {
89+ $ ( "#title" ) . text ( title ) ;
90+ } ;
7791
7892var fetch_info = function ( cb ) {
7993 $ . get ( matrixLogin . endpoint , function ( response ) {
8094 var serverAcceptsPassword = false ;
81- var serverAcceptsCas = false ;
8295 for ( var i = 0 ; i < response . flows . length ; i ++ ) {
8396 var flow = response . flows [ i ] ;
84- if ( "m.login.cas" === flow . type ) {
85- matrixLogin . serverAcceptsCas = true ;
86- console . log ( "Server accepts CAS" ) ;
87- }
8897 if ( "m.login.sso" === flow . type ) {
8998 matrixLogin . serverAcceptsSso = true ;
9099 console . log ( "Server accepts SSO" ) ;
@@ -102,7 +111,7 @@ var fetch_info = function(cb) {
102111matrixLogin . onLoad = function ( ) {
103112 fetch_info ( function ( ) {
104113 if ( ! try_token ( ) ) {
105- show_login ( ) ;
114+ show_login ( false ) ;
106115 }
107116 } ) ;
108117} ;
0 commit comments