| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const migration = require('./migration'); | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function checkAuth(req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-12-27 18:23:24 -05:00
										 |  |  |     if (!await sql.isUserInitialized()) { | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  |         res.redirect("setup"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!req.session.loggedIn && !utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  |         res.redirect("login"); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     else if (!await sql.isDbUpToDate()) { | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  |         res.redirect("migration"); | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  |         next(); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  | async function checkAuthForMigrationPage(req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  |     if (!req.session.loggedIn && !utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         res.redirect("login"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         next(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | async function checkApiAuth(req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-11-30 23:29:21 -05:00
										 |  |  |     if (!req.session.loggedIn) { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         res.status(401).send("Not authorized"); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     else if (await sql.isDbUpToDate()) { | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  |         next(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         res.status(409).send("Mismatched app versions"); // need better response than that
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  | async function checkApiAuthForMigrationPage(req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-11-30 23:29:21 -05:00
										 |  |  |     if (!req.session.loggedIn) { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         res.status(401).send("Not authorized"); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         next(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  | async function checkAppNotInitialized(req, res, next) { | 
					
						
							| 
									
										
										
										
											2017-12-27 18:23:24 -05:00
										 |  |  |     if (await sql.isUserInitialized()) { | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  |         res.status(400).send("App already initialized."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         next(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     checkAuth, | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  |     checkAuthForMigrationPage, | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     checkApiAuth, | 
					
						
							| 
									
										
										
										
											2017-12-03 22:29:23 -05:00
										 |  |  |     checkApiAuthForMigrationPage, | 
					
						
							|  |  |  |     checkAppNotInitialized | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | }; |