( req: IncomingMessage, secure = false )
| 143 | * Pure request of auth session |
| 144 | */ |
| 145 | export async function getAuthSession( |
| 146 | req: IncomingMessage, |
| 147 | secure = false |
| 148 | ): Promise<Session | null> { |
| 149 | const protocol = secure ? 'https:' : 'http:'; |
| 150 | const url = createActionURL( |
| 151 | 'session', |
| 152 | protocol, |
| 153 | // @ts-expect-error |
| 154 | new Headers(req.headers), |
| 155 | process.env, |
| 156 | authConfig.basePath |
| 157 | ); |
| 158 | |
| 159 | if (!req.headers.cookie) { |
| 160 | logger.warn('No cookie in request, can not get auth session:', { |
| 161 | protocol, |
| 162 | headers: req.headers, |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | const response = await Auth( |
| 167 | new Request(url, { headers: { cookie: req.headers.cookie ?? '' } }), |
| 168 | authConfig |
| 169 | ); |
| 170 | |
| 171 | const { status = 200 } = response; |
| 172 | |
| 173 | // Read text first to avoid "Body has already been read" error |
| 174 | const raw = await response.text(); |
| 175 | |
| 176 | let data; |
| 177 | try { |
| 178 | data = JSON.parse(raw); |
| 179 | } catch (error) { |
| 180 | logger.error('Failed to parse auth session response:', error); |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | if (!data || !Object.keys(data).length) { |
| 185 | logger.error('Can not get info, auth session raw:', { |
| 186 | raw, |
| 187 | protocol, |
| 188 | headers: req.headers, |
| 189 | }); |
| 190 | return null; |
| 191 | } |
| 192 | |
| 193 | if (status === 200) { |
| 194 | return data; |
| 195 | } |
| 196 | throw new Error(data.message); |
| 197 | } |
| 198 | |
| 199 | function toAdapterUser( |
| 200 | user: Pick< |
no test coverage detected