mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	fix(mobile): detection when authentication is disabled (closes #1660)
This commit is contained in:
		
							parent
							
								
									ce4b5b8193
								
							
						
					
					
						commit
						c422c3e5b9
					
				
							
								
								
									
										2
									
								
								src/express.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								src/express.d.ts
									
									
									
									
										vendored
									
									
								
							| @ -20,6 +20,8 @@ export declare module "express-serve-static-core" { | |||||||
|             "trilium-component-id"?: string; |             "trilium-component-id"?: string; | ||||||
|             "trilium-local-now-datetime"?: string; |             "trilium-local-now-datetime"?: string; | ||||||
|             "trilium-hoisted-note-id"?: string; |             "trilium-hoisted-note-id"?: string; | ||||||
|  | 
 | ||||||
|  |             "user-agent"?: string; | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -3,30 +3,3 @@ import "../stylesheets/bootstrap.scss"; | |||||||
| // @ts-ignore - module = undefined
 | // @ts-ignore - module = undefined
 | ||||||
| // Required for correct loading of scripts in Electron
 | // Required for correct loading of scripts in Electron
 | ||||||
| if (typeof module === 'object') {window.module = module; module = undefined;} | if (typeof module === 'object') {window.module = module; module = undefined;} | ||||||
| 
 |  | ||||||
| const device = getDeviceType(); |  | ||||||
| console.log("Setting device cookie to:", device); |  | ||||||
| setCookie("trilium-device", device); |  | ||||||
| 
 |  | ||||||
| function setCookie(name: string, value?: string) { |  | ||||||
|     const date = new Date(Date.now() + 10 * 365 * 24 * 60 * 60 * 1000); |  | ||||||
|     const expires = "; expires=" + date.toUTCString(); |  | ||||||
| 
 |  | ||||||
|     document.cookie = name + "=" + (value || "") + expires + "; path=/"; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| function getDeviceType() { |  | ||||||
|     if (window.location.search === "?desktop") return "desktop"; |  | ||||||
|     if (window.location.search === "?mobile") return "mobile"; |  | ||||||
|     return isMobile() ? "mobile" : "desktop"; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // https://stackoverflow.com/a/73731646/944162
 |  | ||||||
| function isMobile() { |  | ||||||
|     const mQ = matchMedia?.("(pointer:coarse)"); |  | ||||||
|     if (mQ?.media === "(pointer:coarse)") return !!mQ.matches; |  | ||||||
| 
 |  | ||||||
|     if ("orientation" in window) return true; |  | ||||||
|     const userAgentsRegEx = /\b(Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|webOS|IEMobile)\b/i; |  | ||||||
|     return userAgentsRegEx.test(navigator.userAgent); |  | ||||||
| } |  | ||||||
|  | |||||||
| @ -17,8 +17,7 @@ import type BNote from "../becca/entities/bnote.js"; | |||||||
| 
 | 
 | ||||||
| function index(req: Request, res: Response) { | function index(req: Request, res: Response) { | ||||||
|     const options = optionService.getOptionMap(); |     const options = optionService.getOptionMap(); | ||||||
| 
 |     const view = getView(req); | ||||||
|     const view = !isElectron && req.cookies["trilium-device"] === "mobile" ? "mobile" : "desktop"; |  | ||||||
| 
 | 
 | ||||||
|     //'overwrite' set to false (default) => the existing token will be re-used and validated
 |     //'overwrite' set to false (default) => the existing token will be re-used and validated
 | ||||||
|     //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
 |     //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
 | ||||||
| @ -61,6 +60,38 @@ function index(req: Request, res: Response) { | |||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function getView(req: Request): "desktop" | "mobile" { | ||||||
|  |     // Electron always uses the desktop view.
 | ||||||
|  |     if (isElectron) { | ||||||
|  |         return "desktop"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Respect user's manual override via URL.
 | ||||||
|  |     if ("desktop" in req.query) { | ||||||
|  |         return "desktop"; | ||||||
|  |     } else if ("mobile" in req.query) { | ||||||
|  |         return "mobile"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Respect user's manual override via cookie.
 | ||||||
|  |     const cookie = req.cookies?.["trilium-device"]; | ||||||
|  |     if (cookie === "mobile" || cookie === "desktop") { | ||||||
|  |         return cookie; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Try to detect based on user agent.
 | ||||||
|  |     const userAgent = req.headers["user-agent"]; | ||||||
|  |     if (userAgent) { | ||||||
|  |         // TODO: Deduplicate regex with client-side login.ts.
 | ||||||
|  |         const mobileRegex = /\b(Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|webOS|IEMobile)\b/i; | ||||||
|  |         if (mobileRegex.test(userAgent)) { | ||||||
|  |             return "mobile"; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return "desktop"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function getThemeCssUrl(theme: string, themeNote: BNote | null) { | function getThemeCssUrl(theme: string, themeNote: BNote | null) { | ||||||
|     if (theme === "auto") { |     if (theme === "auto") { | ||||||
|         return `${assetPath}/stylesheets/theme.css`; |         return `${assetPath}/stylesheets/theme.css`; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran