6
6
using Nullinside . Api . Common . Twitch ;
7
7
using Nullinside . Api . Model ;
8
8
using Nullinside . Api . Model . Shared ;
9
+ using Nullinside . Api . Shared . Support ;
9
10
10
11
namespace Nullinside . Api . TwitchBot . Controllers ;
11
12
@@ -56,23 +57,28 @@ public LoginController(INullinsideContext dbContext, IConfiguration configuratio
56
57
/// </returns>
57
58
[ AllowAnonymous ]
58
59
[ HttpGet ]
59
- [ Route ( "twitch-login" ) ]
60
60
public async Task < IActionResult > TwitchLogin ( [ FromQuery ] string code , [ FromServices ] ITwitchApiProxy api , CancellationToken token ) {
61
61
string ? siteUrl = _configuration . GetValue < string > ( "Api:SiteUrl" ) ;
62
62
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 } ") ;
64
64
}
65
65
66
66
string ? email = await api . GetUserEmail ( token ) ;
67
67
if ( string . IsNullOrWhiteSpace ( email ) ) {
68
- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?error=4 ") ;
68
+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?error={ TwitchBotLoginErrors . TwitchAccountHasNoEmail } ") ;
69
69
}
70
70
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 ) ;
72
78
if ( string . IsNullOrWhiteSpace ( bearerToken ) ) {
73
- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?error=2 ") ;
79
+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?error={ TwitchBotLoginErrors . InternalError } ") ;
74
80
}
75
81
76
- return Redirect ( $ "{ siteUrl } /twitch-bot/login ?token={ bearerToken } ") ;
82
+ return Redirect ( $ "{ siteUrl } /twitch-bot/config ?token={ bearerToken } ") ;
77
83
}
78
84
}
0 commit comments