Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can SSR pages access to request? #401

Closed
tatemz opened this issue Sep 22, 2021 · 2 comments
Closed

Can SSR pages access to request? #401

tatemz opened this issue Sep 22, 2021 · 2 comments

Comments

@tatemz
Copy link
Contributor

tatemz commented Sep 22, 2021

How would one go about performing an authenticated backend request using the ssr feature for pages? It seems that this would be impossible without access to cookies or other request headers.

Below is an example of a possible additional parameter to the props function. Am I off base here?

import React from 'https://esm.sh/react'
import type { SSROptions } from 'https://deno.land/x/aleph/types.d.ts'

export const ssr: SSROptions = {
  props: async (router, request) => {
    const { blogPostID } = router.params;

    // This is an example of how an access token or any other value could
    // Be pulled from a request object (via cookies, headers, etc)
    const accessToken = getAccessTokenFromRequest(request);

    const response = await fetch(`/api/blogPosts/${blogPostID}`, {
      headers: { "x-access-token": accessToken }
    });

    const blogPost = await response.json();

    return {
      blogPost,
    };
  },
  paths: async () => {
    return []
  }
}

export default function Page(props) {
  return (
    <p>Title: {props.blogPost.title}</p>
  )
}
@ije
Copy link
Member

ije commented Sep 22, 2021

i am working on it, check #364

ije added a commit that referenced this issue Sep 23, 2021
* Update types

* Allow `useDeno` and `ssr.props` access `Request` (close #22, #364, #401)

* Rename
@ije
Copy link
Member

ije commented Sep 24, 2021

supported in beta.18

@ije ije closed this as completed Sep 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants