66using Nullinside . Api . Common . Twitch ;
77using Nullinside . Api . Model ;
88using Nullinside . Api . Model . Shared ;
9+ using Nullinside . Api . Shared . Support ;
910
1011namespace Nullinside . Api . TwitchBot . Controllers ;
1112
@@ -56,23 +57,28 @@ public LoginController(INullinsideContext dbContext, IConfiguration configuratio
5657 /// </returns>
5758 [ AllowAnonymous ]
5859 [ HttpGet ]
59- [ Route ( "twitch-login" ) ]
6060 public async Task < IActionResult > TwitchLogin ( [ FromQuery ] string code , [ FromServices ] ITwitchApiProxy api , CancellationToken token ) {
6161 string ? siteUrl = _configuration . GetValue < string > ( "Api:SiteUrl" ) ;
6262 if ( null == await api . CreateAccessToken ( code , token ) ) {
63- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?error=3 ") ;
63+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?error={ TwitchBotLoginErrors . TwitchErrorWithToken } ") ;
6464 }
6565
6666 string ? email = await api . GetUserEmail ( token ) ;
6767 if ( string . IsNullOrWhiteSpace ( email ) ) {
68- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?error=4 ") ;
68+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?error={ TwitchBotLoginErrors . TwitchAccountHasNoEmail } ") ;
6969 }
7070
71- string ? bearerToken = await UserHelpers . GetTokenAndSaveToDatabase ( _dbContext , email , token ) ;
71+ ( string ? id , string ? username ) user = await api . GetUser ( token ) ;
72+ if ( string . IsNullOrWhiteSpace ( user . username ) || string . IsNullOrWhiteSpace ( user . id ) ) {
73+ return Redirect ( $ "{ siteUrl } /twitch-bot/config?error={ TwitchBotLoginErrors . InternalError } ") ;
74+ }
75+
76+ string ? bearerToken = await UserHelpers . GetTokenAndSaveToDatabase ( _dbContext , email , token , api . OAuth ? . AccessToken ,
77+ api . OAuth ? . RefreshToken , api . OAuth ? . ExpiresUtc , user . username , user . id ) ;
7278 if ( string . IsNullOrWhiteSpace ( bearerToken ) ) {
73- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?error=2 ") ;
79+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?error={ TwitchBotLoginErrors . InternalError } ") ;
7480 }
7581
76- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?token={ bearerToken } ") ;
82+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?token={ bearerToken } ") ;
7783 }
7884}
0 commit comments