@@ -10,10 +10,8 @@ import (
1010
1111 "golang.org/x/oauth2"
1212
13- "github.com/theandrew168/bloggulus/backend/model"
14- "github.com/theandrew168/bloggulus/backend/postgres"
13+ "github.com/theandrew168/bloggulus/backend/command"
1514 "github.com/theandrew168/bloggulus/backend/random"
16- "github.com/theandrew168/bloggulus/backend/repository"
1715 "github.com/theandrew168/bloggulus/backend/web/page"
1816 "github.com/theandrew168/bloggulus/backend/web/util"
1917)
@@ -134,7 +132,7 @@ func HandleOAuthSignIn(conf *oauth2.Config) http.Handler {
134132
135133func HandleOAuthCallback (
136134 secretKey string ,
137- repo * repository. Repository ,
135+ cmd * command. Command ,
138136 conf * oauth2.Config ,
139137 fetchUserID FetchUserID ,
140138) http.Handler {
@@ -174,54 +172,16 @@ func HandleOAuthCallback(
174172 }
175173
176174 username := util .HashUserID (userID , secretKey )
177- account , err := repo .Account ().ReadByUsername (username )
178- if err != nil {
179- if ! errors .Is (err , postgres .ErrNotFound ) {
180- util .InternalServerErrorResponse (w , r , err )
181- return
182- }
183-
184- // We need to create a new account at this point.
185- account , err = model .NewAccount (username )
186- if err != nil {
187- util .InternalServerErrorResponse (w , r , err )
188- return
189- }
190-
191- err = repo .Account ().Create (account )
192- if err != nil {
193- slog .Error ("failed create user account" , "error" , err .Error ())
194- util .BadRequestResponse (w , r )
195- return
196- }
197-
198- slog .Info ("account created" ,
199- "account_id" , account .ID (),
200- )
201- }
202-
203- // Create a new session for the account.
204- session , sessionID , err := model .NewSession (account , util .SessionCookieTTL )
175+ sessionID , err := cmd .SignIn (username )
205176 if err != nil {
206177 util .InternalServerErrorResponse (w , r , err )
207178 return
208179 }
209180
210- err = repo .Session ().Create (session )
211- if err != nil {
212- util .CreateErrorResponse (w , r , err )
213- return
214- }
215-
216181 // Set a permanent cookie after sign in.
217182 sessionCookie := util .NewPermanentCookie (util .SessionCookieName , sessionID , util .SessionCookieTTL )
218183 http .SetCookie (w , & sessionCookie )
219184
220- slog .Info ("account signed in" ,
221- "account_id" , account .ID (),
222- "session_id" , session .ID (),
223- )
224-
225185 next := "/"
226186 nextCookie , err := r .Cookie (util .NextCookieName )
227187 if err == nil {
@@ -235,7 +195,7 @@ func HandleOAuthCallback(
235195 })
236196}
237197
238- func HandleDebugSignIn (secretKey string , repo * repository. Repository ) http.Handler {
198+ func HandleDebugSignIn (secretKey string , cmd * command. Command ) http.Handler {
239199 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
240200 // Generate a random userID for the debug sign in.
241201 userID , err := random .BytesBase64 (16 )
@@ -247,54 +207,16 @@ func HandleDebugSignIn(secretKey string, repo *repository.Repository) http.Handl
247207 userID = "debug_" + userID
248208 username := util .HashUserID (userID , secretKey )
249209
250- account , err := repo .Account ().ReadByUsername (username )
251- if err != nil {
252- if ! errors .Is (err , postgres .ErrNotFound ) {
253- util .InternalServerErrorResponse (w , r , err )
254- return
255- }
256-
257- // We need to create a new account at this point.
258- account , err = model .NewAccount (username )
259- if err != nil {
260- util .InternalServerErrorResponse (w , r , err )
261- return
262- }
263-
264- err = repo .Account ().Create (account )
265- if err != nil {
266- slog .Error ("failed create user account" , "error" , err .Error ())
267- util .BadRequestResponse (w , r )
268- return
269- }
270-
271- slog .Info ("account created" ,
272- "account_id" , account .ID (),
273- )
274- }
275-
276- // Create a new session for the account.
277- session , sessionID , err := model .NewSession (account , util .SessionCookieTTL )
210+ sessionID , err := cmd .SignIn (username )
278211 if err != nil {
279212 util .InternalServerErrorResponse (w , r , err )
280213 return
281214 }
282215
283- err = repo .Session ().Create (session )
284- if err != nil {
285- util .CreateErrorResponse (w , r , err )
286- return
287- }
288-
289216 // Set a permanent cookie after sign in.
290217 sessionCookie := util .NewPermanentCookie (util .SessionCookieName , sessionID , util .SessionCookieTTL )
291218 http .SetCookie (w , & sessionCookie )
292219
293- slog .Info ("account signed in" ,
294- "account_id" , account .ID (),
295- "session_id" , session .ID (),
296- )
297-
298220 next := "/"
299221 nextCookie , err := r .Cookie (util .NextCookieName )
300222 if err == nil {
@@ -308,7 +230,7 @@ func HandleDebugSignIn(secretKey string, repo *repository.Repository) http.Handl
308230 })
309231}
310232
311- func HandleSignOutForm (repo * repository. Repository ) http.Handler {
233+ func HandleSignOutForm (cmd * command. Command ) http.Handler {
312234 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
313235 // Check for a session ID. If there isn't one, just redirect back home.
314236 sessionID , err := r .Cookie (util .SessionCookieName )
@@ -321,23 +243,11 @@ func HandleSignOutForm(repo *repository.Repository) http.Handler {
321243 cookie := util .NewExpiredCookie (util .SessionCookieName )
322244 http .SetCookie (w , & cookie )
323245
324- // Lookup the session by its client-side session ID.
325- session , err := repo .Session ().ReadBySessionID (sessionID .Value )
326- if err != nil {
327- switch {
328- case errors .Is (err , postgres .ErrNotFound ):
329- http .Redirect (w , r , "/" , http .StatusSeeOther )
330- default :
331- util .InternalServerErrorResponse (w , r , err )
332- }
333- return
334- }
335-
336- // Delete the session from the database.
337- err = repo .Session ().Delete (session )
246+ err = cmd .SignOut (sessionID .Value )
338247 if err != nil {
339248 switch {
340- case errors .Is (err , postgres .ErrNotFound ):
249+ case errors .Is (err , command .ErrSessionNotFound ):
250+ // If the session was not found, just redirect back home.
341251 http .Redirect (w , r , "/" , http .StatusSeeOther )
342252 default :
343253 util .InternalServerErrorResponse (w , r , err )
0 commit comments